Search code examples
androidandroid-layouttextviewandroid-tablelayout

Only one TextView occurs in TableLayout


I need one in the left and the right. All configurations should be done by code. Screenshot:

enter image description here

Code:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.overview);
    loadData();
    TableLayout tl = (TableLayout)findViewById(R.id.tl);
    TableRow tr = new TableRow(this);
    TableRow tr2 = new TableRow(this);
    tr.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    //tr2.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    //tr2.setLayoutParams(new ViewGroup.LayoutParams(L., LayoutParams.WRAP_CONTENT));
    TextView tv = new TextView(this);
    TextView tv2 = new TextView(this);
    //tv.se
    tv.setGravity(Gravity.LEFT);
    tv2.setGravity(Gravity.RIGHT);
    tv.setText("Test");
    tv2.setText("Test");
    //tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    //tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    //tv.setTextSize(50);
    //tv2.setTextSize(50);
    tr.addView(tv);
    tr.addView(tv2);
    //tl.addView(tr);
    //setContentView(tl);
    tl.addView(tr, new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    }

Layout:

<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/tl"
  android:stretchColumns="*">
</TableLayout>

Solution

  • I think you have to use:

      android:stretchColumns="0,1"
    

    Great Example can be found here

    do this:

    remove 1f from tv2 layoutparams.

    tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    this may solve you problem according to given link.