0

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.

enter image description here

mark
  • 503
  • 5
  • 27

1 Answers1

0

Try this

TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = ViewGroup.LayoutParams.FILL_PARENT;
textView.setLayoutParams(params);
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • I have already tried this : GridLayout.LayoutParams lp = new GridLayout.LayoutParams(); lp.width = LayoutParams.FILL_PARENT; lp.height = LayoutParams.FILL_PARENT; lp.setGravity(Gravity.FILL); tv.setLayoutParams(lp); But no luck :'( – mark May 01 '15 at 07:05
  • @ Murtaza Khursheed Hussain I have updated my question and added screenshot of my issue . – mark May 01 '15 at 07:22
  • gridlayout is deprecated, best is you can use a LinearLayout with TextView inside it having width set to 0dp and weight to 0.25, like this you can have equal widths boxes. – Murtaza Khursheed Hussain May 01 '15 at 07:32
  • I need to use col span and row span properties, that's why I used GridLayout – mark May 01 '15 at 08:27