Show Include multiple Layouts in single one activity XML layout file android

How to add insert show multiple XML layout files into single activity_main.xml file.

In this tutorial we are going to learn a new awesome widget function of android programming language called as <include layout=””>.  This widget is the sub layout including widget which will gives us the functionality to add anther layout file into our current existing layout file so we can Show Include multiple Layouts in single one activity XML layout file inside our android application. So here is the complete step by step tutorial for add multiple layouts into single activity_main.xml file.

android-project-download-code-button

How to Show Include multiple Layouts in single one activity XML layout file android.

Code for MainActivity.java file.

package com.example.includemultiplelayouts_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

 <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"
 tools:context="com.example.includemultiplelayouts_android_examples.com.MainActivity"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="This Text is coming from activity_main.xml file."
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center"
 android:padding="5dp"
 android:background="#F44336"
 android:textColor="#FFFFFF"
 android:textStyle="bold" />
 
 <include layout="@layout/second_layout"/>
 
 <include layout="@layout/third_layout"/>
 
 <include layout="@layout/forth_layout"/>

</LinearLayout>

Code for second_layout.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical" >
 
 <TextView
 android:id="@+id/textView2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="This Text is coming from second_layout.xml file."
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center"
 android:padding="5dp"
 android:background="#EA80FC"
 android:textColor="#FFFFFF"
 android:textStyle="bold" />

</LinearLayout>

Code for third_layout.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical" >
 
 <TextView
 android:id="@+id/textView3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="This Text is coming from third_activity.xml file."
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center"
 android:padding="5dp"
 android:background="#2196F3"
 android:textColor="#FFFFFF"
 android:textStyle="bold" />

</LinearLayout>

Code for forth_layout.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >
 
 <TextView
 android:id="@+id/textView4"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="This Text is coming from forth_activity.xml file."
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center"
 android:padding="5dp"
 android:background="#009688"
 android:textColor="#FFFFFF"
 android:textStyle="bold" />

</LinearLayout>

Screenshot:

Show Include multiple Layouts in single one activity XML layout file android

Click here to download Show Include multiple Layouts in single one activity XML layout file android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *