I would like to load TableRows with TextViews into the table outside of oncreate. It should look like that.
TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
tableLayout = (TableLayout)findViewById(R.id.itemTable);
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(getApplicationContext());
tv.setText("Dynamic Textview");
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(tv);
tableLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
The problem is that I want to do the whole thing outside of the oncreate function. The rows and text views should only be added at runtime. If I do it outside then the added elements are rendered but not displayed. Is it even possible to load that outside of onCreate?
The xml looks like that:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/itemTable"
android:layout_width="703dp"
android:layout_height="241dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@color/orange"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
The function i want to work with:
public void setView () {
TableRow tr = new TableRow(getContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int i = 0; i < 5; i++) {
TextView tv = new TextView(getContext());
tv.setText("Dynamic Textview");
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(tv);
}
tr.invalidate();
tableLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableLayout.invalidate();
}
Advertisement
Answer
This helped:
runOnUiThread(new Runnable() {
public void run() {
}
});
