how to set width of tittle row of a table? - android

TableLayout tl = new TableLayout(ActivityAddFlashcard.this);
TableRow tittleRow = new TableRow(ActivityAddFlashcard.this);
TableRow fRow = new TableRow(ActivityAddFlashcard.this);
TableRow.LayoutParams params = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.addView(getTextView("کلمه"));
fRow.addView(getTextView("معنی"));
TextView tv = new TextView(ActivityAddFlashcard.this);
tv.setLayoutParams(new TableRow.LayoutParams(tl.getWidth() // <---- //return 0
, TableRow.LayoutParams.WRAP_CONTENT));
tv.setText("dksvlfsnfmmmmnv");
tittleRow.addView(tv);
tittleRow.setGravity(Gravity.CENTER);
tl.addView(tittleRow);
fRow.addView(new TextView(ActivityAddFlashcard.this));
tl.addView(fRow);
TableLayout.LayoutParams params2 = new TableLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
, LinearLayout.LayoutParams.WRAP_CONTENT);
fRow.setGravity(Gravity.CENTER);
fRow.setLayoutParams(params);
tl.setLayoutParams(params2);
tl.setGravity(Gravity.CENTER);
this is my table layout, i want to set the width of the tittle row equal to width of table.
but getWidth return 0 .

Your view can change its width time to time during layouting process. Probably you're looking for MATCH_PARENT solution:
tv.setLayoutParams(
new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
)
);
You may be interested to setup MATCH_PARENT as layoutParameter for tittleRow as well.

Related

how to set width of column dynamically in android tablelayout?

I'm having a tablelayout, where columns are added dynamically. I'm unable to set width of the column/TextView dynamically.
This is my code:
TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);
TableRow header = new TableRow(this);
header.setId(100);
header.setBackgroundColor(Color.rgb(255, 165, 0));
//header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView empCodeHeader = new TextView(this);
empCodeHeader.setId(200);
empCodeHeader.setText("Employee Code");
empCodeHeader.setTextSize(16);
empCodeHeader.setPadding(2,2,2,2);
//empCodeHeader.setWidth(200);
header.addView(empCodeHeader);
TextView empNameHeader = new TextView(this);
empNameHeader.setId(201);
empNameHeader.setText("Employee Name");
empNameHeader.setTextSize(16);
empNameHeader.setPadding(2,2,2,2);
//empNameHeader.setWidth(300);
header.addView(empNameHeader);
I'm able to set using setWidth, but i want to set it dynamically, say 30% for column1 and 70% for column2.
Use LayoutParams for set height and width dynamically.

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);

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