Skip to content Skip to sidebar Skip to footer

Android Relativelayout Alignment Based On View Added Programmatically

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

Solution 1:

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>

Post a Comment for "Android Relativelayout Alignment Based On View Added Programmatically"