Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert layout to layout in android?

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>
like image 836
kongkea Avatar asked Aug 22 '12 11:08

kongkea


2 Answers

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);
like image 197
ColdFire Avatar answered Oct 14 '22 19:10

ColdFire


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>
like image 21
Suragch Avatar answered Oct 14 '22 20:10

Suragch