Although this looks a lot repeated question but first time for me. I searched all over and could not get the result and ended up posting here.
I am creating a table dynamically of which the TableLayout
part is written in xml part.
<RelativeLayout
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@color/colorPrimaryDark"
android:id="@+id/componentA">
<TableLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:id="@+id/tl_componentA">
</TableLayout>
</RelativeLayout>
I created an object for the table
TableLayout tableLayoutA;
tableLayoutA= (TableLayout)view.findViewById(R.id.tl_componentA);
Now I tried to add a row dynamically from here onwards as
createTableRowColumn(tableLayoutA);
Functions Related are
private void createTableRowColumn(TableLayout tableLayout){
TableRow tableRow1= new TableRow(this.context);
tableRow1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
setTableRowColumnProperty(tableRow1);
tableLayout.addView(tableRow1);
}
private void setTableRowColumnProperty(TableRow tableRow){
TableRow.LayoutParams layoutParams= new TableRow.LayoutParams(70, 40);
tableRow.setLayoutParams(layoutParams);
}
I did all this and nothing showed me in the emulater. But when I gave same structure in xml mannually then thing was working well.
For this reason i tried something to figure out
Toast.makeText(getContext(), tableLayoutA.getHeight()+"", Toast.LENGTH_LONG).show();
In toast it was showing me 0. I could not get why, although I have fixed the size for tableLayoutA in the xml itself.