xav
xav

Reputation: 5618

Android: dynamic Layout change in Dialog

I'd like to programmatically change the layout of a Dialog: changing the root LinearLayout orientation. This root LinearLayout contains two layouts: GT and DI. It works great if I use AlertDialog, but not if I use Dialog directly (using Dialog with a custom style because I don't want any title or title space).

Here is my XML layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/GT_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GT"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup
        android:id="@+id/GT_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:height="5dp"
            android:text="GT #1" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:height="5dp"
            android:text="GT #2" />

    </RadioGroup>
</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DI_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DI"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbarAlwaysDrawVerticalTrack="true" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <RadioGroup
                android:id="@+id/DI_group"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <RadioButton
                    android:id="@+id/DI_rdb_1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="DI #1" />

                <RadioButton
                    android:id="@+id/DI_rdb_2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="DI #2" />
            </RadioGroup>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

And here is my code:

LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootLayout = inflater.inflate(R.layout.new_game, (ViewGroup) activity.findViewById(R.id.root_container));

if (screenIsLandscape) {
   ((LinearLayout) rootLayout).setOrientation(LinearLayout.HORIZONTAL);
} else {
   ((LinearLayout) rootLayout).setOrientation(LinearLayout.VERTICAL);
}

Dialog dlg = new Dialog(activity, R.style.FullHeightDialog);
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
dlg.setContentView(R.layout.my_layout);
dlg.show();

I don't want to use "layout-land" to avoid duplicating XML (my XML is rather big but simplified here for this post). Thank you in advance.

Upvotes: 0

Views: 7495

Answers (1)

Silver
Silver

Reputation: 12475

You created new object "rootLayout" from xml-file, then changed it (rootLayout, not xml). Now this object is not linked with your file.

You have a changed object, use it:

dlg.setContentView(rootLayout);

Upvotes: 3

Related Questions