in Android layout_weight for Table Rows in dynamic Table - android

I'm dynamically creating Table rows and adding two TextViews into the new Rows. Doing that works fine, but now I'm trying to format the LayoutParams so that the two TextViews have a layout_weight of 1 each. I've tried adding some LayoutParams but it looks like they are being ignored. Here's my code for creating the rows, and I've commented out the LayoutParams bit, and there is no change when I run the app, which says to me that the LayoutParams are being ignored. I need some help assigning layout_weight to the two TextViews so that in my row-creating code I can achieve this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1" />
So here's my code to create the rows:
TableLayout tl = (TableLayout) findViewById(R.id.pres_table);
for (int ctr = 0; ctr < pres_res.size(); ctr++)
{
final String pst_str = pres_res.get(ctr);
final String yrs_str = yrs_res.get(ctr);
TableRow tr = new TableRow(this);
/*
TableRow.LayoutParams trPara = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
int leftMargin = 10;
int topMargin = 2;
int rightMargin = 10;
int bottomMargin = 2;
trPara.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trPara);
*/
TextView tv1 = new TextView(this);
tv1.setText(pst_str);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(18);
TextView tv2 = new TextView(this);
tv2.setText(yrs_str);
tv2.setTextColor(Color.BLACK);
tv2.setTextSize(18);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
and the XML to create the Table is basic like this:
<TableLayout
android:id="#+id/pres_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
</TableLayout>
Thanks!

// try this way
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
tv1.setText(pst_str);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(18);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
tv2.setText(yrs_str);
tv2.setTextColor(Color.BLACK);
tv2.setTextSize(18);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));

First, thanks to Geobits for setting me on the right path and letting me know I had the LayoutParams calls in the wrong place. They need to be applied to the TextView, not the Table Row. Then based on that, I found a StackOverflow posting: Set the layout weight of a TextView programmatically where the second answer was the solution for me. So now my code looks like this and it works the way I want it:
TableLayout tl = (TableLayout) findViewById(R.id.pres_table);
for (int ctr = 0; ctr < pres_res.size(); ctr++)
{
final String pst_str = pres_res.get(ctr);
final String yrs_str = yrs_res.get(ctr);
TableRow tr = new TableRow(this);
/*
TableRow.LayoutParams trPara = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
int leftMargin = 10;
int topMargin = 2;
int rightMargin = 10;
int bottomMargin = 2;
trPara.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trPara);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
*/
LayoutParams tvPar = new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT, 1f);
TextView tv1 = new TextView(this);
tv1.setLayoutParams(tvPar);
tv1.setText(pst_str);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(18);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(tvPar);
tv2.setText(yrs_str);
tv2.setTextColor(Color.BLACK);
tv2.setTextSize(18);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
Right under the commented out bit is the solution showing the new LayoutParams tvPar code.

Related

Width column tablelayout dynamically

I have a problem regarding not using XML in the TableLayout.
Someone has already asked the same as I do but the answer provided has been unable to help me.
how to set width of column dynamically in android tablelayout?
Right now I have this
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
Button a = new Button(this);
a.setText("Dynamic Button");
a.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
a.setGravity(2);
tr.addView(a);
But I fail to see how changing
tr.setLayoutParams
Will do the job of making for example the first button column 70% and the other button the 30%
You can create an linear layout with weight and keep those two buttons inside linear layout and then set linear to your table row.
Check below code:
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
params = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
params.weight = 1;
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.setWeightSum(1);
/* Create a Button to be the row-content. */
LinearLayout.LayoutParams chiledParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams.weight = (float) 0.7;
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(chiledParams);
/* Add Button to row. */
LinearLayout.LayoutParams chiledParams1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams1.weight = (float) 0.3;
Button a = new Button(this);
a.setText("Button");
a.setLayoutParams(chiledParams1);
layout.addView(b);
layout.addView(a);
tr.addView(layout);
tl.addView(tr);
you need setLayoutParams to your widget like following code:
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width,heigh);
b.setLayoutParams(tlp);
TableRow.LayoutParams tlp1 = new TableRow.LayoutParams(width/3,heigh/3);
a.setLayoutParams(tlp1);

Android Adding more than one TextView to a TableRow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}

View not showing in layout when layoutparams is set programatically

Every time I set the layout param of TextView to wrap content or fill parent it's not showing in the view, but when I set it to pixels it's working why is that?
Here is the relevant code:
items = (LinearLayout) findViewById(R.id.llmain);
tb = new TableLayout(MainActivity.this);
tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tb.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(MainActivity.this);
ImageButton ima = new ImageButton(MainActivity.this);
ImageButton imr = new ImageButton(MainActivity.this);
TextView tvin = new TextView(this);
TableRow.LayoutParams trp = new TableRow.LayoutParams();
trp.setMargins(0, 0, 0, 10);
tr.setLayoutParams(trp);
tr.setGravity(Gravity.CENTER);
ima.setBackgroundColor(Color.TRANSPARENT);
ima.setImageResource(R.drawable.add);
imr.setBackgroundColor(Color.TRANSPARENT);
imr.setImageResource(R.drawable.remove);
tvin.setWidth(100);
tvin.setHeight(45);
tvin.setGravity(Gravity.CENTER);
tvin.setText(sil[i]);
tvin.setBackgroundColor(Color.rgb(200, 0, 255));
tr.addView(ima);
tr.addView(tvin);
tr.addView(imr);
tb.addView(tr);
items.addView(tb);
I tried using this
tvin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
and this
tvin.setWidth(LayoutParams.FILL_PARENT);
tvin.setHeight(LayoutParams.FILL_PARENT);
but none of these work.
Try:
tvin.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
and remove the tvin.setWidth and height.
You should always set the layout params of view's parent, TableRow in this case.
MATCH_PARENT makes no difference but it's a new name and you should be using it instead of FILL_PARENT.
In this Line u have to use height and width .. TableRow.LayoutParams trp = new TableRow.LayoutParams(); Like this
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
Try to use the LinearLayout.LayoutParams
TextView tvin = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tvin.setLayoutParams(llp);

Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
Afterwards, the TableLayout looks something along the lines of:
January$500
February$500
March$405
But I want it to look like:
January $500
February $500
March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
yawus this is a good tutorial will help you set the table layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this);
row.setId(tag);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
rlp.setMargins(0, 0, 0, 0);
row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.

Android: Programmatically Setting width of Child View using Percentage

I've read this article:
Setting width of Child View using Percentage
So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?
Thanks
You should use TableRow.LayoutParams and not Linearlayout.LayoutParams
trDetails.addView(tvInfo, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));
trDetails.addView(tvDetails, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));

Categories

Resources