Frion3L
Frion3L

Reputation: 1522

How to program relativeLayouts in code

I need to create a dinamic table which will be fit with rows of data. The rows I want will have an specific form, like this:

http://i.imgur.com/1D8ge.png

So, to get it, I used relativeLayouts. The problem is that I first tried it with an .xml to see what parameters will have the differents views that it forms, and I get it, but now I cant replicate it in the code.

Here is my code:

TableLayout linkRoutesTable = (TableLayout) findViewById(R.id.layoutLinkRoutes);

//ROW:
TableRow linkRouteRow = new TableRow(this);
linkRouteRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linkRouteRow.setId(i)                   

//RELATIVELAYOUT:
float px0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(0, (int)px0)));

//CONTENT:

//  BUTTON: ...             
//  NUMBER:...
//  ADDRESS:...
//  NAME:   ...                 

relativeLayout.addView(number);
relativeLayout.addView(name);
relativeLayout.addView(address);
relativeLayout.addView(button);

linkRouteRow.addView(relativeLayout);
linkRouteTable.addView(linkRouteRow);
linkRoutesTable.refreshDrawableState();

It doesn't show anything...

The .xml show ok, here it is:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" android:orientation="vertical">

   <TableRow>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


                    android:layout_width="0dp"
                    android:layout_weight="1"
    android:id="@+id/layoutLinkRoutes" >


    <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button>
    <TextView android:textStyle="bold" android:textSize="45dp" android:text="1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"  android:layout_marginLeft="22dp"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/textView1" android:layout_marginLeft="16dp"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignLeft="@+id/textView3"></TextView>

</RelativeLayout>

</TableRow>

What Am I doing wrong in the code? I need some help, I tried everything.

If I dont use the relativeLayouts and the rows, it shows something but bad... so the problem is the rows and the relativelayout

Really thanks...

Upvotes: 0

Views: 109

Answers (1)

Flo
Flo

Reputation: 27455

You don't have to define the layout in code to fill a table dynamically. You can infalte a layout which you define in xml like a normal layout.

So you define your table row layout one time in a xml file and infalte it multiple times into your table layout.

Check this answer.

Upvotes: 1

Related Questions