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
Related
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.
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);
...
I am a starter in android application development. Kindly help me how to create multiple rows in a table and key performance to create a row. I am able to create 3 rows now. but i want to autogenerate the rows while entering the datas in table.
try this:
//Declare mytable in layout XML file.
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
//loop through number of times u want to create row.
for (int current = 0; current < numofrowsrequired; current++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//add required views with required parameters
TextView labelTV = new TextView(this);
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
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);
I am creating table rows programmatically and i want to put a view that i created before in a row. Is there any way to do it?
for ( current = 0; current < rapor1.length; current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView f1 = new TextView(this);
f1.setId(200+current);
f1.setText(rapor1[current]);
f1.setTextColor(Color.BLACK);
f1.setWidth(100);
tr.addView(f1);
DrawView dv = new DrawView(this);
tr.addView(dv);
it should be working but something is wrong.
I think it may helps. Just change this string:
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
And of course you need to set LayoutParams to any view that you want to show on your screen