Introduction
Android contains several types of layout(i.e. GridLayout, LinearLayout, RelativeLayout, etc). In this article, I will discuss the fragment layout. Fragments are just like a Sub Activity. It has its own lifecycle, input events, and which we can add or remove while the activity is running. We can use multiple fragments in a single activity. We can also use a fragment in multiple activities. The lifecycle of a fragment is directly attached to its activity. Suppose we if destroy an activity then it's all fragments will be destroyed automatically or we pause the activity then it's all fragments will be pause automatically.
Now we create a simple project to understand the concept of Fragment Layout.
We will create a project like below,
When we click on any item of Listview. It will show the android OS name and its version using textview.
In this project, we will use 3 Activity.
The first activity is activity_main
This is our main activity. In this activity, we use two fragment layout. In the first fragment layout, we will show the data of Listview. This Listview is present in other activity(list_fragment activity). In the second fragment layout, we will show the data of TextView. This TextView is present in other activity(text_fragment).
Xml code of activity_main activity is,

- <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="horizontal"
- tools:context="com.amal.fragments.MainActivity">
- <fragment
- android:layout_height="match_parent"
- android:layout_width="120dp"
- class="com.example.fragmentlayout.MenuFragment"
- android:id="@+id/fragment"/>
- <fragment
- android:layout_width="200dp"
- android:layout_height="match_parent"
- class="com.example.fragmentlayout.TextFragment"
- android:id="@+id/fragment2"/>
- </LinearLayout>

- <?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:orientation="vertical" >
- <Listview
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@android:id/list" />
- </LinearLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:gravity="center"
- android:background="#5ba4e5"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25px"
- android:textColor="#ffffff"
- android:layout_gravity="center"
- android:text="@string/textview1"
- android:id="@+id/AndroidOs"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textColor="#ffffff"
- android:text="@string/textview2"
- android:textSize="15px"
- android:id="@+id/Version"/>
- </LinearLayout>

- package com.example.fragmentlayout;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- public class MainActivity1 extends FragmentActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
- package com.example.fragmentlayout;
- import android.os.Bundle;
- import android.support.v4.app.ListFragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.Listview;
- public class MenuFragment extends ListFragment {
- String[] AndroidOS = new String[] {
- "Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream SandWich", "Jelly Bean", "KitKat", "LollyPop"
- };
- String[] Version = new String[] {
- "1.5", "1.6", "2.0-2.1", "2.2", "2.3", "3.0-3.2", "4.0", "4.1-4.3", "4.4", "5.0"
- };@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.list_fragment, container, false);
- ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_list_item_1, AndroidOS);
- setListAdapter(adapter);
- return view;
- @Override
- public void onListItemClick(Listview l, View v, int position, long id) {
- TextFragment txt = (TextFragment) getFragmentManager().findFragmentById(R.id.fragment2);
- txt.change(AndroidOS[position], "Version : " + Version[position]);
- getListview().setSelector(android.R.color.holo_blue_dark);
- }
- }
- View view =inflater.inflate(R.layout.list_fragment, container, false);
- public void onListItemClick(Listview l, View v, int position, long id) {
- TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2);
- txt.change(AndroidOS[position],"Version : "+Version[position]);
- getListview().setSelector(android.R.color.holo_blue_dark);
- }
- package com.example.fragmentlayout;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class TextFragment extends Fragment {
- TextView text, vers;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.text_fragment, container, false);
- text = (TextView) view.findViewById(R.id.AndroidOs);
- vers = (TextView) view.findViewById(R.id.Version);
- return view;
- }
- public void change(String txt, String txt1) {
- text.setText(txt);
- vers.setText(txt1);
- }
- }



Pankaj Kumar ChoudharyPosted Jul 27, 2015, 8:23 PM
Thanks Santhakumar Sir............
Santhakumar MunuswamyPosted Jul 27, 2015, 2:36 PM
Thanks for nice article:)
Pankaj Kumar ChoudharyPosted Jul 27, 2015, 11:22 AM
Thanks Rajeesh Sir......
Rajeesh MenothPosted Jul 27, 2015, 8:30 AM
Good One
SharadPosted Jul 27, 2015, 6:23 AM
good one
Gowtham RajamanickamPosted Jul 27, 2015, 5:19 AM
good one
Pankaj Kumar ChoudharyPosted Jul 27, 2015, 4:06 AM
Thanks Gopi Sir........
Gopi ChandPosted Jul 27, 2015, 12:46 AM
Good article
Sibeesh VenuPosted Jul 27, 2015, 12:42 AM
Nice Share. Thanks
RakeshPosted Jul 26, 2015, 11:52 PM
Nice one