1

I have programmatically created a TableLayout called DataTable (similar to Create TableLayout programmatically). Now I need to programmatically add the table to a RelativeLayout. I try the following:

DataTable dataTable = new DataTable(getApplicationContext(), data);
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.rel_view);
rootLayout.addView(dataTable);

The dataTable shows but the alignment is messed up. My problem now is to set the other components in relation to dataTable.

Below I am showing what I want to end up with. Except, my_table is where dataTable is supposed to go.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel_view"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MyViewActivity" >

    <TableLayout
        android:id="@+id/my_table"
        android:layout_alignParentTop="true" />

    <Spinner
    android:id="@+id/my_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_table" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_table"
    android:layout_toRightOf="@id/my_spinner"
    android:text="@string/button_mine"
    android:onClick="buttonPushed" />

</RelativeLayout>

One thought would be to basically keep the xml layout the way I have it, and then somehow replace my_table with dataTable, keeping the id and the layout_alignParentTop fields. I don't know how to do that though.

Another thought would be to use

RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(LayoutParams.blahblah)

but I can't figure how to set the alignments. Also, again, there is no id for the other views to relate to.

If the above is not clear, my main question is this: In a RelativeLayout, how do I align xml hard-coded elements (Button and Spinner) with respect to a programmatically added element (TableLayout)?

Community
  • 1
  • 1
learner
  • 11,490
  • 26
  • 97
  • 169

1 Answers1

0

You could add an extra item to your layout (either a ViewGroup or a LinearLayout) that serves as the container for the dynamically created View you have. Align the Spinner and Button to this extra item. Instead of adding your view directly to the root view of the layout, you add it to the container. It should always keep the alignment you set in the xml layout file. For example, this layout should always keep your DataTable below your Spinner:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel_view"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MyViewActivity" >

    <TableLayout
        android:id="@+id/my_table"
        android:layout_alignParentTop="true" />

    <Spinner
    android:id="@+id/my_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_table" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_table"
    android:layout_toRightOf="@id/my_spinner"
    android:text="@string/button_mine"
    android:onClick="buttonPushed" />

    <LinearLayout
        android:id="@+id/my_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@id/my_spinner"/>

</RelativeLayout>
ebarrenechea
  • 3,775
  • 1
  • 31
  • 37
  • Wow! That was simple! Basically I replace the `TableLayout` in your answer with the `LinearLayout` you show. – learner Mar 14 '13 at 23:12