I am trying to add TextView to GridLayout at Run Time. As there is no property to set GridLayout's Cells background, so I am setting TextView Background looks like cell background having stroke at its border. My problem is that when adding TextView to Gridlayout at run time, TextView size is not Stretching equal to GridLayout Cell .
In xml file when I set :
<GridLayout
android:id="@+id/gridLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignmentMode="alignBounds"
android:columnCount="5"
android:columnOrderPreserved="false"
android:useDefaultMargins="false" >
<TextView
android:layout_gravity="fill"
android:background="@drawable/grid_lay_cell_bg"
android:padding="5dp"
android:text="Name"
android:textSize="14dp" />
</GridLayout>
it worked perfectly, TextView covers complete cell size, but when I am trying adding it run time in following code:
TextView tv3 = new TextView(getActivity());
GridLayout.LayoutParams lp3 = new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(0, 1));
lp3.setGravity(Gravity.FILL);
tv3.setLayoutParams(lp3);
tv3.setGravity(Gravity.FILL);
tv3.setPadding(padd, padd, padd, padd);
tv3.setBackgroundResource(R.drawable.grid_lay_cell_bg);
tv3.setTextSize(16);
tv3.setText("Test");
gridLay.addView(tv3, lp3);
TextView not covering complete cell size.
Please give me some hint, how to stretch TextView size equal to cell size when adding TextView to GridLayout at run time in Android.