Programmatic Android Layout - android

I'm trying to correlate how the Android XML looks versus creating it programmatically and I'm failing miserably. I've read several references stating that the type for the LayoutParams for the View must be equal to those of the parent, but I'm just not grokking it.
Given the following XML, how would I re-create this programmatically?
<TableRow
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:paddingTop="3dp"
android:orientation="vertical"
>
<EditText
android:padding="2dp"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_column="0"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="15dp"
android:numeric="integer"
/>
</LinearLayout>
<LinearLayout
android:layout_height="35dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:padding="2dp"
android:orientation="vertical"
>
<CheckBox android:text="Check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox"
android:textColor="#color/Gray"
/>
<CheckBox android:text="Check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox"
android:textColor="#color/Gray"
/>
</LinearLayout>
Here's what I've tried:
// Get the TableLayout
TableLayout tl = (TableLayout)findViewById(R.id.score_table);
LayoutParams lp;
TableRow.LayoutParams tp;
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout ll0_1 = new LinearLayout(this);
// If the layout params of the child should be of
// the type of the parent this should be
// TableRow.LayoutParams?
tp = (TableRow.LayoutParams)tr.getLayoutParams();
tp.height = TableRow.LayoutParams.WRAP_CONTENT;
tp.width = TableRow.LayoutParams.FILL_PARENT;
tp.gravity = Gravity.CENTER_VERTICAL;
// TableRow.LayoutParams has no padding so this
// gets set on the LinearLayout?
ll0_1.setPadding(0,3,0,0);
ll0_1.setLayoutParams(tp);
tr.addView(ll0_1);
EditText et0 = new EditText(this);
et0.setId(100);
et0.setInputType(InputType.TYPE_CLASS_NUMBER);
// Same as above; LP set to type of parent
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f);
lp.height = 35;
lp.width = 100;
// Same as above; the parent's type doesn't have
// these attributes
et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
et0.setPadding(0, 3, 0, 0);
et0.setLayoutParams(lp);
ll0_1.addView(et0);
LinearLayout ll0_2 = new LinearLayout(this);
ll0_2.setOrientation(LinearLayout.VERTICAL);
tr.addView(ll0_2);
CheckBox cb0_1 = new CheckBox(this);
cb0_1.setTextSize(10f);
cb0_1.setTextColor(Color.GRAY);
cb0_1.setBackgroundResource(R.drawable.checkbox_background);
cb0_1.setButtonDrawable(R.drawable.checkbox);
cb0_1.setText("Nil");
ll0_2.addView(cb0_1);
CheckBox cb0_2 = new CheckBox(this);
cb0_2.setTextSize(10f);
cb0_2.setTextColor(Color.GRAY);
cb0_2.setBackgroundResource(R.drawable.checkbox_background);
cb0_2.setButtonDrawable(R.drawable.checkbox);
cb0_2.setText("10fer");
ll0_2.addView(cb0_2);
// Duplicate code removed for brevity
//...
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
There is no error, but it does not match the XML. Instead, it looks like this:
The first one is from the XML, the second one is my attempt at reproducing it programmatically.

LayoutInflater is the easiest way :
public View myView(){
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.myview, null);
TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
return v;
}

Related

Defining ImageView to be aligned right

Tried to align imageview to right.. code crashes. why?
UPDATED: Here is all row im having problem with. I think i failed on determining layouts somewhere here
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParam
s.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textView1.setText(names);
tableRow1.addView(textView1);
ImageView icon;
icon = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);
// icon.setLayoutParams(params);
icon.setImageResource(R.drawable.ic_done); //default value
switch (value) {
case "1":
icon.setImageResource(R.drawable.ic_done);
break;
case "0":
icon.setImageResource(R.drawable.ic_bad);
default:
break;
}
System.out.println("value : " + value);
icon.setPadding(densityToPixels(13), 0, 0, 0);
tableRow1.addView(icon);
try in xml
<RelativeLayout
android:id="#+id/beam_contact_entry_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/beam_contact_entry_invite"
android:background="?entry_background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_toRightOf="#+id/contact_info_avatar" >
<TextView
android:id="#+id/beam_contact_fragment_entry_text"
style="?primary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alexander Great"
android:textSize="18sp" />
<TextView
android:id="#+id/beam_contact_fragment_entry_text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-4dp"
android:text="mobile"
android:visibility="gone"
style="?secondary_text"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/beam_contact_entry_invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/beam_contact_entry_contact"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/beam_contact_entry_contact"
android:background="?entry_background"
android:orientation="horizontal" >
<ImageView
android:id="#+id/beam_contact_fragment_entry_right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?entry_background"
android:duplicateParentState="true"
android:src="#drawable/sv_svyaznoy_contact" />
</LinearLayout>
First you defined the ImageView dynamically like
ImageView icon = new ImageView(this);
then you are trying to get the LayoutParam through icon.getLayoutParams(); which doesn't have any LayoutPram because you haven't set any.
To make it work replace
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)icon.getLayoutParams();
with
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);

GridView with different number of columns according to the number of row

I am trying this for 2 days,I am trying to make UIs for the view having two columns in first row,then in second row having three columns,and in third again two columns and so on..And also I have to implement dynamic data(means num of rows/columns are dynamic).
I don't have any idea to implement this type of view.I have also try stragged grid view but this is for dynamic views..but in this I have static 2,3,2,3... columns for 1,2,3,4..rows.Please help me.Any tutorial will be most helpful.
Finally I have solved this problem.I have tried with Raghunandan and user3278416's suggestion.And did all the stuff in runtime except Table layout.Here is the xml file:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
>
And the the two layouts one is for small layout(the second row) and one is for large one(first row):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_offerrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
The small one:
<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="#drawable/big_product_bg" >
<ImageView
android:id="#+id/img_offersrowlayout"
android:layout_width="90dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/img_bydefault" />
<TextView
android:id="#+id/txt_title_homesmallrowlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/img_offersrowlayout"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="BenQ Digital camera"
android:textColor="#color/green_app"
android:textSize="15dp"
android:textStyle="bold" />
Now the Java Code:
int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;
int j = 0;
while (Show.size()>j) {
try {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow.LayoutParams param = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
LinearLayout layout1 = new LinearLayout(getApplicationContext());
TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);
question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));
View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));
View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));
View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));
View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
if (gett) {
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
// trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
layout1.addView(question);
layout1.addView(question1);
tr.addView(layout1);
tablee.addView(tr);
gett = false;
j = j+2;
}
else
{
TableRow tr = new TableRow(getApplicationContext());
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,1);
layout1.addView(question_small);
layout1.addView(question1_small);
layout1.addView(question2_small);
tr.addView(layout1);
tablee.addView(tr);
gett = true;
j = j+3;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here Show is the arraylist of hashmap type and tablee is the tablelayout object which I have made in Xml file.

How to add new row dynamically to table layout

In my android app, I have a table layout. I want to dynamically insert rows. But the problem is the rows are being added horizontally. I want each tablerow to be stacked vertically. I have this so far:
TableLayout mytable = (TableLayout)findViewById(R.id.studentContainer);
TableRow tablerow = new TableRow(this);
tablerow.setOrientation(TableRow.VERTICAL);
EditText g = new EditText(this);
g.setText("AAAAAAAAA");
g.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g2 = new EditText(this);
g2.setText("BBBBBBBBBB");
g2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g);
tablerow.addView(g2);
mytable.addView(tablerow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
TableRow tablerow2 = new TableRow(this);
tablerow2.setOrientation(TableRow.VERTICAL);
EditText g4 = new EditText(this);
g4.setText("AAAAAAAAA");
g4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
EditText g5 = new EditText(this);
g5.setText("BBBBBBBBBB");
g5.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tablerow.addView(g4);
tablerow.addView(g5);
mytable.addView(tablerow2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
XML
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/studentContainer" >
</TableLayout >
Does anyone know whats wrong here?
Thanks
Try to put the first row in the xml and rotate the entire table, edit the text_views if necessary. Like this
XML
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:rotation="-90"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column1"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column2"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column3"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Column4"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
and then code like this
TableRow tr = new TableRow(this);
tr.setPadding(0,20,0,20);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
TextView tv_item4 = new TextView(this);
tv_item1.setText(M_Number);
tv_item1.setGravity(Gravity.CENTER);
tv_item2.setText(M_Number);
tv_item2.setGravity(Gravity.CENTER);
tv_item3.setText(M_Number);
tv_item3.setGravity(Gravity.CENTER);
tv_item4.setText(M_Number);
tv_item4.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
tr.addView(tv_item4);
Table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Better you have a table row item in XML, inflate in as a new view and set data accordingly.
then add that view to the table layout

Programmatically add content to a TableLayout on Android

I am trying to add rows to a table after a click to a button (so after the generation of the activity).
My XML code is:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
android:id="#+id/result_table">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col1" />
<TextView
style="#style/MyHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Col2" />
</TableRow>
</TableLayout>
So when I load the activity I have my table with my only row. Then when I click on my button I do:
resultTable = (TableLayout)findViewById(R.id.result_table);
LayoutParams rowLayoutParams = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams cellLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow tableRow;
TextView cellValue;
for(Asdf a : as) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(rowLayoutParams);
cellValue = new TextView(this);
cellValue.setText(a.getText());
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
cellValue = new TextView(this);
cellValue.setText("asdf");
cellValue.setLayoutParams(cellLayoutParams);
cellValue.setTextAppearance(this, R.style.MyText);
tableRow.addView(cellValue);
resultTable.addView(tableRow);
}
resultTable.invalidate();
But it doesn't add anything, and I am sure about that the loop iterates. I don't get any warning/exception in the logcat.
Can you help me please ??
Try to change the LayoutParams for the added Views like this:
TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams cellLayoutParams = TableRow.new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

RelativeLayout Elements are incorrectly overlapping

I'm trying to build a part of a Android View Programmatically, but unfortunately I'm having some problems with the RelativeLayout. it makes my Elements overlay on eachother
This is my Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_layout);
LinearLayout ll = (LinearLayout) findViewById(R.id.container);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// fill content
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView score = new TextView(this);
score.setText(""+i);
RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
score.setLayoutParams(lpScore);
TextView description = new TextView(this);
description.setText("this is my description");
RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
description.setLayoutParams(lpDescription);
CheckBox checkbox = new CheckBox(this);
RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(score);
rl.addView(description);
rl.addView(checkbox);
tl.addView(rl);
}
ll.addView(tl);
This is what it looks like:
As you can see, the "description" is on top of of the "score".
Here is the same code in xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayoutTextEntry" >
<TableRow
android:id="#+id/tableRowTextEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="score" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/score"
android:text="description" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="select" />
</RelativeLayout>
</TableRow>
</TableLayout>
And here is what it looks like in xml:
As you can see, in xml, the toRightOf-command works as expected - the description is to the right of the score
How is that possible? Shouldnt the line
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
do the same thing?
At the moment you are calling it, score.getId() will return -1 (invalid), because it has not been added yet to the screen with the whole layout processed.
You will have to set the id manually with setId() before calling getId() and it should all work fine.

Categories

Resources