Fragment Layout In Android

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,
 
project
 
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,
  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="horizontal"  
  7.     tools:context="com.amal.fragments.MainActivity">  
  8.     <fragment  
  9.     android:layout_height="match_parent"  
  10.     android:layout_width="120dp"  
  11.     class="com.example.fragmentlayout.MenuFragment"  
  12.     android:id="@+id/fragment"/>  
  13.     <fragment  
  14.     android:layout_width="200dp"  
  15.     android:layout_height="match_parent"  
  16.     class="com.example.fragmentlayout.TextFragment"  
  17.     android:id="@+id/fragment2"/>  
  18. </LinearLayout>  
Graphical Layout of activity_main is,
 
Layout of activity
 
Second Activity is list_fragment.xml,
 
In this activity we a Listview. We will show the data of Listview in fragment Layout of activity_main activity. Xml code of list_fragment activity is:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.     <Listview  
  8.     android:layout_width="match_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:id="@android:id/list" />  
  11. </LinearLayout>  
Graphical Layout of this activity is,
 
activity
 
Our third Activity is text_fragment.xml,
 
In this activity we use two TextView. In first TextView we will show the name of android OS and in second TextView we will show the version of android OS . We show the data of both textview in fragment layout of activity_main activity. Xml code of text_fragment activity is.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:gravity="center"  
  7.     android:background="#5ba4e5"  
  8.     android:layout_height="match_parent">  
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="25px"  
  13.         android:textColor="#ffffff"  
  14.         android:layout_gravity="center"  
  15.         android:text="@string/textview1"  
  16.         android:id="@+id/AndroidOs"/>  
  17.     <TextView  
  18.     android:layout_width="wrap_content"  
  19.     android:layout_height="wrap_content"  
  20.     android:layout_gravity="center"  
  21.     android:textColor="#ffffff"  
  22.     android:text="@string/textview2"  
  23.     android:textSize="15px"  
  24.     android:id="@+id/Version"/>  
  25. </LinearLayout>  
Graphical Layout of text_fragment activity is,
 
Graphical Layout
 
Now we move to coding part of this project.
 
As I mentioned above we are using three activities in this project. So I will explain each activity one by one. First of all, we will see the source code(java code ) of activity_main layout.
 
MainActivity1.java
 
This java file contains the source code for the activity_main layout. Content of this java file is:
  1. package com.example.fragmentlayout;  
  2. import android.os.Bundle;  
  3. import android.support.v4.app.FragmentActivity;  
  4.   
  5. public class MainActivity1 extends FragmentActivity {  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.     }  
  12. }  
We can see that the above source code doesn’t contain any method or event. Only we extend(inherit) the FragmentActivity class in the above source code. FragmentActivity is a Base class for activities that want to use the support-based fragment API. We are using fragments in this activity to show the content(controls) of other activities. So the functionality of these controls is present in their corresponding activity.
 
MenuFragment.java
 
This java file contains source code for list_fragment activity. Source code of this activity is.
  1. package com.example.fragmentlayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.ListFragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.Listview;  
  10.   
  11. public class MenuFragment extends ListFragment {  
  12.     String[] AndroidOS = new String[] {  
  13.         "Cupcake""Donut""Eclair""Froyo""Gingerbread""Honeycomb""Ice Cream SandWich""Jelly Bean""KitKat""LollyPop"  
  14.     };  
  15.     String[] Version = new String[] {  
  16.         "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"  
  17.     };@Override  
  18.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  19.         View view = inflater.inflate(R.layout.list_fragment, container, false);  
  20.         ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_list_item_1, AndroidOS);  
  21.   
  22.         setListAdapter(adapter);  
  23.         return view;  
  24.   
  25.   
  26.         @Override  
  27.         public void onListItemClick(Listview l, View v, int position, long id) {  
  28.             TextFragment txt = (TextFragment) getFragmentManager().findFragmentById(R.id.fragment2);  
  29.             txt.change(AndroidOS[position], "Version : " + Version[position]);  
  30.             getListview().setSelector(android.R.color.holo_blue_dark);  
  31.         }  
  32.   
  33.     }  
In the above code we create two arrays of string types. The first array contains information about the android os name and the second array contains information about the android version. We use an onCreateView method in this java file that use 3 parameters. The first parameter is the LayoutInflater object. LayoutInflater is used to dynamically add and remove control in the activity. The second parameter is ViewGroup. ViewGroup is used as a parent container for any view. The third parameter is Bundle Object. When we pass data between two activity then this data is passed in the form of bundles.
  1. View view =inflater.inflate(R.layout.list_fragment, container, false);  
In the above code, we use a inflate method. Inflate method read all xml code of activity and convert it into a View type object. We use an ArrayAdapter object in code. An arrayadapter is used to bind the data with Listview.
  1. public void onListItemClick(Listview l, View v, int position, long id) {  
  2.    TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2);  
  3.    txt.change(AndroidOS[position],"Version : "+Version[position]);  
  4.    getListview().setSelector(android.R.color.holo_blue_dark);  
  5. }  
This method will invoke when we click on any item of Listview. In this code we Create an object of TextFragment class. Using this object we call the change method of TextFragement class. We pass two parameter in this method. First one is android OS name and second one is android version.
 
TextFragment.java- Source code of this java file is:
  1. package com.example.fragmentlayout;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.TextView;  
  9.   
  10. public class TextFragment extends Fragment {  
  11.     TextView text, vers;  
  12.   
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  15.         View view = inflater.inflate(R.layout.text_fragment, container, false);  
  16.         text = (TextView) view.findViewById(R.id.AndroidOs);  
  17.         vers = (TextView) view.findViewById(R.id.Version);  
  18.         return view;  
  19.     }  
  20.     public void change(String txt, String txt1) {  
  21.         text.setText(txt);  
  22.         vers.setText(txt1);  
  23.     }  
  24. }  
In above code we extend(inherit) Fragment class. We also create two object of TextView control. text object contain the reference of AndroidOs textview and vers object contain the reference of Version textview.
 
We create a change method. This method will invoke from MenuFragment class. Using this method we insert the text in both textview.
 
Now our project is ready to run. If we click on any item of Listview then it will show the name of android OS and Its version as below:
 
android OS
 
Listview


Similar Articles