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);
Related
I followed the example at https://stackoverflow.com/a/7123217/2884981 but unfortunately, the row is not extending for the full width of the table (see how the black "border"/background is so large on the right while it's only 1px wide everywhere else).
I want the second column to extend, so it's taking up all the available space to the right.
Here is the code I am using. Please, if anyone knows how I can get that right "border" to 1px instead of it looking so awful, let me know.
for (int i = 0; i < tasks.size(); i++)
{
TaskDetail taskDetail = tasks.get(i);
TableRow row = new TableRow(this);
// Set the background/border colour to black
row.setBackgroundColor(Color.BLACK);
row.setPadding(1, 1, 1, 1);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0,0,1,0);
LinearLayout detailCell = new LinearLayout(this);
detailCell.setBackgroundColor(Color.parseColor("#FFE6F0"));
detailCell.setLayoutParams(layoutParams);
LinearLayout valueCell = new LinearLayout(this);
valueCell.setBackgroundColor(Color.WHITE);
valueCell.setLayoutParams(layoutParams);
TextView detailName = new TextView(this);
detailName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
detailName.setText(taskDetail.getName());
detailName.setPadding(5, 10, 5, 5);
detailName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView valueName = new TextView(this);
valueName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
valueName.setText(taskDetail.getValue());
valueName.setPadding(5, 10, 5, 5);
valueName.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
detailCell.addView(detailName);
valueCell.addView(valueName);
// Add to the screen layout
row.addView(detailCell);
row.addView(valueCell);
table.addView(row);
}
Set weight to be 1.0f for each column in the row as follows:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
Simply make changes as follows:
...
...
row.setPadding(1, 1, 1, 1);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
layoutParams.setMargins(0,0,1,0);
LinearLayout detailCell = new LinearLayout(this);
...
...
I am adding TextView dynamically using for loop to LinearLayout which orientation is horizontal. Is it possible to align the TextView to second line if it reach end of screen width?
You can do it in the following manner:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
LinearLayout layout1 = new LinearLayout(this);
LinearLayout layout2 = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout1.setOrientation(LinearLayout.HORIZONTAL);
layout2.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 3; i++) {
TextView lblView = new TextView(this);
TextView lblView2 = new TextView(this);
lblView.setLayoutParams(params);
lblView.setText("hello");
lblView.setEms(5);
lblView.setGravity(Gravity.CENTER);
lblView2.setLayoutParams(params);
lblView2.setText("hello");
lblView2.setEms(5);
lblView2.setGravity(Gravity.CENTER);
layout1.addView(lblView);
layout2.addView(lblView2);
}
layout.addView(layout1);
layout.addView(layout2);
setContentView(layout);
The following will do the trick,
textview.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
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.
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));
I have an Achievement dialog, and would like this:
And this is my code, each achievement is a relative layout, and all are contained in a LinearLayout (pnlAchievement):
RelativeLayout pnlItem = new RelativeLayout(Parent);
pnlItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView txtItemName = new TextView(Parent);
txtItemName.setText(ACHIEVEMENTS_NAME[i]);
txtItemName.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView txtAchived = new TextView(Parent);
txtAchived.setText(Achived[i] ? "Achieved!" : "Not Achieved!");
RelativeLayout.LayoutParams rlpAchived = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpAchived.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlpAchived.addRule(RelativeLayout.ALIGN_RIGHT, txtItemName.getId());
txtAchived.setLayoutParams(rlpAchived);
TextView txtDescription = new TextView(Parent);
txtDescription.setText(ACHIEVEMENTS_DESCRIPTION[i]);
txtDescription.setHorizontallyScrolling(false);
RelativeLayout.LayoutParams rlpDesc = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpDesc.addRule(RelativeLayout.ALIGN_BOTTOM, txtItemName.getId());
rlpDesc.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
txtDescription.setLayoutParams(rlpDesc);
pnlItem.addView(txtItemName);
pnlItem.addView(txtAchived);
pnlItem.addView(txtDescription);
pnlAchievement.addView(pnlItem);
It work well list all the achievements, but the description is NOT below the Name, it is OVER the Name, like this:
And, can you please tell me how to add the line between every item without using Canvas (this is not very important, but if there is, it's better)?
You need to use RelativeLayout.BELOW
For the line you can use a Label with a backgroundcolor.