Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inflate a view / layout into another layout?

I'm looking for a way to inflate another layout into the first layout in android. How would one do this? Here are the two XML files. The first is the main layout, the second is the layout I would like to inflate into the first.

I can't just include the layout as I will use this method to inflate other layouts into wire frames later on.

Code also at: http://pastebin.com/wjZ4s1cs as stackoverflow does not like XML.

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff000000"
    >
    <TextView
        android:id="@+id/headerMenuText"
        android:text="@string/main_menu_title"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="16pt"
        android:paddingTop="10px"
        android:paddingBottom="10px"
        android:gravity="center"
    >
    </TextView>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_y="100dip"
        android:gravity="center"
        android:layout_gravity="center"
    >
        <TableRow
            android:gravity="center"
            android:layout_gravity="center">
            <ImageView
                android:id="@+id/menuItem1"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_settings"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem2"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_system_restart"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem3"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_history"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem4"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_correction"
            ></ImageView>

            <ImageView
                android:id="@+id/menuItem5"
                android:layout_height="101dip"
                android:layout_width="89dip"
                android:src="@drawable/icon_game_other"
            ></ImageView>
        </TableRow>

        <TableRow android:gravity="center">
            <TextView
                android:id="@+id/menuItemText1"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_1"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText2"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_2"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText3"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_3"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText4"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_4"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

            <TextView
                android:id="@+id/menuItemText5"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/main_menu_item_text_5"
                android:layout_gravity="center"
                android:gravity="center"
            ></TextView>

        </TableRow>
    </TableLayout>
    <View or layout
        android:id="@+id/screen_layout_bottom_menu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
    </View or layout>
</AbsoluteLayout>

Second layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen_bottom_menu"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    >
    <TableLayout
        android:id="@+id/screen_bottom_menu_table"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    >
        <TableRow>
            <ImageView
                android:id="@+id/screen_bottom_menu_button_back"
                android:src="@drawable/back">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_ok"
                android:src="@drawable/checkmark">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_cancel"
                android:src="@drawable/xmark">
            </ImageView>

            <ImageView
                android:id="@+id/screen_bottom_menu_button_key_toggle"
                android:src="@drawable/lock">
            </ImageView>
        </TableRow>
    </TableLayout>
</LinearLayout>
like image 458
Kevin Parker Avatar asked Mar 17 '11 16:03

Kevin Parker


Video Answer


2 Answers

There is ViewStub but I never used it and I think it can't be used more than once.

You can inflate the menu layout and attach it to the main layout:

AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout);
LayoutInflater inflater = 
              (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);

then when you want to change you can remove it:

mainLayout.removeView(menuLayout);

and add another the same way.

This will work because you want to add the layout as the last child of the parent layout. If you want to add it, say, at 1st position, you can inflate your layout without attaching it to the parent (use false as last arg), and adding it manually specifying the index:

mainLayout.addView(menuLayout, 0);
like image 67
bigstones Avatar answered Oct 17 '22 06:10

bigstones


I'm not sure I realy understood what had you ment by "I can't just include the layout as I will use this method to inflate other layouts into wire frames later on." But if you need the second layout to be put many times in various other layouts, use <include layout="@layout/the_second_layout" /> in these other layouts.

like image 45
ernazm Avatar answered Oct 17 '22 06:10

ernazm