user963395
user963395

Reputation:

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>

Upvotes: 1

Views: 391

Answers (2)

Chimu
Chimu

Reputation: 758

LayoutParams are used by views to tell their parents how they want to be laid out. There are around 13 different types of LayoutParams defined in Android. eg. LinearLayout.Layoutparams, ViewGroup.LayoutParams, ActionBar.LayoutParams etc.

So if one is adding TextView to LinearLayout, it means LinearLayout is the parent of TextView, so while defining layout parameteres for textView one should use LinearLayout.LayoutParams and not ViewGroup.LayoutParams

in your case you are adding two textviews to table row, so but obvious you should use TableRow.LayoutParams

so replace this line

tv2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f)); 

with

tv2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f)); 

Write similarly for tv This was my first post on this site. Hope it will be Helpful :)

Upvotes: 2

Hanry
Hanry

Reputation: 5531

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.

Upvotes: 0

Related Questions