Fragments In Android Applications

In this article, we will learn about working with fragments in Android applications.

Overview

Hope you got a chance to read my previous articles about developing an Android app. I have covered some topics, the links of which are here.

In this article, we will learn about working with fragments in Android applications.

Introduction

First of all, what is a fragment? Fragment is nothing but another form of activity. We create fragments to contain Views, just like activities. An activity typically fills the entire screen, displaying the various Views that make up the user interface of an application. When an activity is displayed in a large screen device, such as on a tablet, it is somewhat out of place. Due to the bigger screen size, all the Views in an activity must be arranged to make full use of the increased space. So, by using mini activities, each activity can have its own set of Views. During run time, an activity can contain one or more of these mini activities and it depends on the screen orientation of the device. These mini activities are nothing but fragments.

Fragments are always embedded in an activity. The following image shows two fragments. In Fragment 1, ListView is showing a list of movie titles. Fragment 2 shows the details of the movie and contains some TextViews and ImageViews showing text and images respectively.

If the application is running either on an Android tablet in portrait mode or on an Android smartphone, Fragment 1 may be embedded in one activity, however, Fragment 2 may be embedded in another activity. When a user selects an item in the list in Fragment 1, activity 2 will be started.

If the application is running in a tablet in landscape mode, both fragments 1 and 2 can be embedded within a single activity.

So, we saw that fragments have an important role to create the user interface of an Android application. Fragments can be added dynamically to the activities, in order to create the best user experience for the target device.

Coding

Configure a new Android project called MyFragmentApp by selecting File-> New-> New Project option.



Click on Next to write the default activity name.



Click on Finish to complete the wizard.

Add first activity as fragments Fragment1Activity, in the project.


Add second activity as fragments Fragment2Activity, in the project.



Layout of the activity_fragment1.xml is,
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:orientation="vertical"  
  6.      android:background="#00FF00"  
  7.      tools:context="com.example.administrator.myfragmentapp.Fragment1Activity">  
  8.    
  9.      <TextView android:text="This is Ist Fragment!"  
  10.          android:layout_width="fill_parent"  
  11.          android:layout_height="wrap_content"  
  12.          android:textColor="#000FFF"  
  13.          android:layout_marginTop="20dp"  
  14.          android:textSize="30dp"  
  15.          />  
  16.  </LinearLayout>  

Layout of the activity_fragment2.xml file is,

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:orientation="vertical"  
  6.      tools:context="com.example.administrator.myfragmentapp.Fragment2Activity">  
  7.    
  8.      <TextView android:text="This is 2nd Fragment!"  
  9.          android:layout_width="fill_parent"  
  10.          android:layout_height="wrap_content"  
  11.          android:layout_marginTop="20dp"  
  12.          android:textSize="30dp"/>  
  13.    
  14.  </LinearLayout>  

Add a method onCreateView() in the Java class file of above activities,

  1. package com.example.administrator.myfragmentapp;  
  2.    
  3.  import android.app.Fragment;  
  4.  import android.os.Bundle;  
  5.  import android.view.LayoutInflater;  
  6.  import android.view.Menu;  
  7.  import android.view.MenuItem;  
  8.  import android.view.View;  
  9.  import android.view.ViewGroup;  
  10.    
  11.    
  12.  public class Fragment1Activity extends Fragment {  
  13.    
  14.      @Override  
  15.      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  16.      {  
  17.          return inflater.inflate(R.layout.activity_fragment1,container,false);  
  18.      }  
  19.  }  
  1. package com.example.administrator.myfragmentapp;  
  2.    
  3.  import android.app.Fragment;  
  4.  import android.support.v7.app.ActionBarActivity;  
  5.  import android.os.Bundle;  
  6.  import android.view.LayoutInflater;  
  7.  import android.view.Menu;  
  8.  import android.view.MenuItem;  
  9.  import android.view.View;  
  10.  import android.view.ViewGroup;  
  11.    
  12.  public class Fragment2Activity extends Fragment {  
  13.    
  14.      @Override  
  15.      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  16.      {  
  17.          return inflater.inflate(R.layout.activity_fragment2,container,false);  
  18.      }  
  19.  }  

Layout of the activity_main.xml file is,

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:orientation="horizontal"  
  6.      android:background="#FFFFFF"  
  7.      tools:context=".MainActivity">  
  8.      <fragment  
  9.          android:name="com.example.administrator.myfragmentapp.Fragment1Activity"  
  10.          android:id="@+id/fragment1"  
  11.          android:layout_width="0px"  
  12.          android:layout_height="match_parent"  
  13.          android:layout_weight="2"/>  
  14.      <fragment  
  15.          android:name="com.example.administrator.myfragmentapp.Fragment2Activity"  
  16.          android:id="@+id/fragment2"  
  17.          android:layout_width="0px"  
  18.          android:layout_height="match_parent"  
  19.          android:layout_weight="2"/>  
  20.  </LinearLayout>  
Result: Execute the application.



Explanation

As we have already seen that fragment behaves very much like an activity, it has a Java class and loads its UI from an XML file. The Java class for a fragment needs to extend the Fragment base class.

  1. public class Fragment1Activity extends Fragment {}  
  2. To draw the UI  
  3. for a fragment, just override the onCreateView() method and it returns a View object  
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   
  5. {  
  6.     return inflater.inflate(R.layout.activity_fragment1, container, false);  
  7. }  
In the above method, we use a LayoutInflater object to inflate the UI from the specified XML file. The container argument refers to the parent ViewGroup, which is the activity in which we are trying to embed the fragment. The savedInstanceState argument enables to restore the fragment to its previously saved state. Adding a fragment to an activity, we use <fragment> element, as shown below,
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:orientation="horizontal"  
  6.      android:background="#FFFFFF"  
  7.      tools:context=".MainActivity">  
  8.      <fragment  
  9.          android:name="com.example.administrator.myfragmentapp.Fragment1Activity"  
  10.          android:id="@+id/fragment1"  
  11.          android:layout_width="0px"  
  12.          android:layout_height="match_parent"  
  13.          android:layout_weight="2"/>  
  14.      <fragment  
  15.          android:name="com.example.administrator.myfragmentapp.Fragment2Activity"  
  16.          android:id="@+id/fragment2"  
  17.          android:layout_width="0px"  
  18.          android:layout_height="match_parent"  
  19.          android:layout_weight="2"/>  
  20.  </LinearLayout>  

Conclusion: 

In this article, we saw how to add fragment to an activity, statically. In the next article, I will explain how to add fragment, dynamically.