12

I've got two layouts first and second, I want to insert second to first. I want to insert second layout into layout that has id @+id/layout when I press button Get Layout, it show the the second layout on the bottom of button

first layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
    android:id="@+id/btn_get_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Layout" />

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>

second layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img_cover"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/card_beauty" />

    <ImageView
        android:id="@+id/img_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/sample_0" />
</RelativeLayout>

</LinearLayout>
kongkea
  • 9,788
  • 12
  • 39
  • 49

5 Answers5

34

if I understood you right you should use the following code within your first layout

<include 
     layout="@layout/second_layout"
     android:id="@+id/includedLayout"
     android:visibility="gone"
     android:layout_below="@id/buttonId" />

and then in your button's action you just use

((RelativeLayout)findViewById(R.id.includedLayout)).setVisibility(View.VISIBLE);
Amir Ismail
  • 3,865
  • 3
  • 20
  • 33
ColdFire
  • 6,764
  • 6
  • 35
  • 51
12
    LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
    getLayoutInflater().inflate(R.layout.second_layout, placeHolder);
Sameer
  • 4,379
  • 1
  • 23
  • 23
  • 1
    If I need to dynamically insert several layouts (the No of layout is unkown beforehand), how can I do it? – LiangWang May 30 '13 at 23:37
11

Use include

Here is a somewhat fuller example. The square blue layout is inserted into the main layout using include.

enter image description here

activity_main.xml

This is the main layout. The custom layout is referenced using include. You can override any of the attributes here, too.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- Here is the inserted layout -->
    <include layout="@layout/my_layout"/>

</RelativeLayout>

my_layout.xml

This is the custom layout that will be inserted into the main layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="5dp"
        android:text="My Layout"/>

</RelativeLayout>
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

You Should use this code snippet to add a Layout inside another one.

    parentLayout=(LinearLayout)findViewById(R.id.parentLayout);
    inflateView=View.inflate(this,R.layout.view_video,parentLayout);

Here the parentLayoutis the root View and R.layout.view_video is the layout that you need to insert.

Febin Mathew
  • 991
  • 1
  • 11
  • 20
0

Use LayoutInflator to inflate an external layout into existing layout. Checkout LayoutInflater on Android Dev Site for more information about it.

Chitranshu Asthana
  • 1,089
  • 8
  • 19