assign weight to 3 TextViews with TableLayout.LayoutParams - android

I have the following in my activity which successfully displays 3 columns of data (there will be up to 10 rows, thus the for loop):
for (int i=0;i<10;i++)
{
TableRow tr = new TableRow(this);
TextView tv0 = new TextView(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
trlp.weight = 1;
tr.setLayoutParams(trlp);
tv0.setText("" + debtName[i]);
tv1.setText("" + debtAmount[i]);
tv2.setText("" + debtPayment[i]);
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
}
I want them to be evenly weighted out as there is a title row that is hard coded in to the layout. The "trlp.weight = 1;" did not change it from when it did not exist.
What I am looking for is a way to get the tv0,tv1,tv2 TextViews to be distributed evenly (horizontally).
Also for reference, I'm running at API 12.

you have to set the weight onto the children that are supposed to scale.
1 means 100% scaling by default, if you want 3 textvies to spread evenly set 0.33 as weight for each.
TableRow.layoutParams trlp = new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.33f);
tv0.setLayoutParams(trlp);
...
tr.add(tv0);
...

Related

Adding TableRows dynamically in Android doesn't work

I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.

Make TableLayout columns to fit perfectly the size of its contents programmatically

I'm trying to make a table in Android (API Level 10 minimum) where some of the columns have to stretch as more as possible whereas some need to shrink to fit its contents' size.
This is what I have:
(the colors are just to see the cells' width and height)
the 'at' column is what I am talking about. I need it to look like this one:
http://img268.imageshack.us/img268/1572/77141b8b8eb443d783cede5.png
Here is the code I am using for now:
ScrollView sv = new ScrollView(this);
TableLayout table = new TableLayout(this);
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
table.setPadding(10, 10, 10, 10);
//title row
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TextView title = new TextView(this);
title.setText("Majors - Season 10");
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 4;
rowTitle.addView(title, params);
//header row
TableRow rowHeaders = new TableRow(this);
TextView away = new TextView(this);
away.setText("Away");
away.setTypeface(Typeface.DEFAULT_BOLD);
away.setGravity(Gravity.CENTER_HORIZONTAL);
rowHeaders.addView(away);
TextView empty = new TextView(this);
empty.setText(" ");
// empty.setGravity(Gravity.CENTER_HORIZONTAL);
empty.setBackgroundColor(Color.GREEN);
rowHeaders.addView(empty);
TextView home = new TextView(this);
home.setText("Home");
home.setTypeface(Typeface.DEFAULT_BOLD);
home.setGravity(Gravity.CENTER_HORIZONTAL);
rowHeaders.addView(home);
TextView score = new TextView(this);
score.setText("Score");
score.setTypeface(Typeface.DEFAULT_BOLD);
score.setGravity(Gravity.RIGHT);
rowHeaders.addView(score);
TableRow rowMatch = createMatchRow();
TableRow rowMatch2 = createMatchRow();
table.addView(rowTitle);
table.addView(rowHeaders);
table.addView(rowMatch);
table.addView(rowMatch2);
sv.addView(table);
return sv;
createMatchRow() is just a method that returns a TableRow for each of the matches. (Rows 2-)
Eh, fixed it by removing the
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
and adding:
table.setColumnStretchable(0, true);
table.setColumnStretchable(2, true);

Android - Dynamic ColSpan not working

I am having two table rows in one of my activity. First row will always contains 2 textview and second might contains one or two textview (it depends on userinput). Whenever I am having one textview in 2nd row, I tried to span it to two columns. I am trying something this
TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams();
param.span = 2;
txtView.setLayoutParams(param);
But its not working. What happens is, second txtview in first row is pushed out of the screen. So can anyone here tell where i am making mistake?
I made this example, hope it helps you.
TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
TableRow row = new TableRow(this);
TableRow row2 = new TableRow(this);
TextView col1, col2, col3;
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
col1 = new TextView(this);
col2 = new TextView(this);
col3 = new TextView(this);
col1.setText("col1");
col2.setText("col2");
col3.setText("col3");
row.addView(col1);
row.addView(col2);
row.addView(col3);
table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//HERE IS WHAT YOU NEED
TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
params.span = 3; //amount of columns you will span
col1 = new TextView(this);
col1.setText("span on col1 makes it bigger than the others");
col1.setLayoutParams(params);
row2.addView(col1);
table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Explaining the answer marked as correct - looks like you need to set layout params after adding the view to it's parent. for instance, add the textview to a row and then set layout params

Align components in TableRow

First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);

How to set the row in a table layout?

So far, I have code that dynamically creates my table within a horizontal scrollview. I want each item to appear in the same y location and so that you can only see one at a time. Right now, each column gets set, but the visual is like a staircase, where the next row is underneath and only moved over about 50px. Is there a way to force each column to take up the entire screen width? Also, can you set the row that the content appears in?
TableLayout tl = (TableLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
ImageView iv = new ImageView(this);
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
else
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.noimage);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tl.addView(tr);
}
As you have use the TableLayout and now you want each cell to consume appropriate same size throughout the table respectfully by comparing itself via row and column.
So, you have to set WEIGHT to each item and specify weight value = "1.0" or below it.

Categories

Resources