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