John Baker
John Baker

Reputation: 435

TextView doesn't expand to match the TableRow height

How do I stretch the TextView so it fills the entire TableRow height? Here is a screenshot of what I'm getting right now,

I want the TextView to expand with the TableRow.

Here is how I create the row:

row = new TableRow(this);

desc = new TextView(this);

desc.setBackgroundResource(R.drawable.borders);
desc.setTypeface(Typeface.SERIF, Typeface.BOLD);  
desc.setText("Production Date");
desc.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

String date = gendataObj.getString("date");
text = new TextView(this);
text.setBackgroundResource(R.drawable.borders);
text.setText(date);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

row.addView(desc);
row.addView(text);

table.addView(row, new TableLayout.LayoutParams(
        TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

Upvotes: 5

Views: 2350

Answers (1)

Piotr Chojnacki
Piotr Chojnacki

Reputation: 6867

Change:

text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));

to:

text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));

Because otherwise, you don't specify height (just width), which by default will remain as WRAP_CONTENT which causes your TextView to be as heigh as it's content (text).

Upvotes: 5

Related Questions