Introduction to Fragments in Android


            Fragments in Android is considered very important when you wanted to build a cleaner and reusable interface across various screen sizes of Android devices. Before explaining about fragments, have you ever seen how Gmail app appears in a 7 inch tablet and 5 inch phone? See the below screens, I hope you got the difference.

Tablet View

Phone View

Before explaining the screens and fragments, let me give a short intro about Activity in android. In simple words activity is nothing but a screen with which the user interacts with. (For detailed info about activity check this).
        By the way Fragment is a portion of your activity which enables you to divide your activity for modular design. Each fragment is an independent module that is tightly bound to the Activity into which it is placed..Each of these fragments have their own life-cycle and UI, however fragment's life-cycle is directly affected by the host's activity life-cycle. The primary advantage of fragments  is the ease with which you can create dynamic and flexible UI designs that can be adapted to suite a range of screen sizes from small screen Android devices to tablets. Fragments were added to Android API in Honeycomb version of Android with API version 11.
         In the above gmail app screen we can very well see in tablet - two fragments (A,B) are added to same Activity (A) and in 5 inch phone fragments (A,B) are added to separate Activities (A,B) respectively.

Creating New Fragments

        Extend the fragment class to create a new Fragment, defining the UI and implementing the functionality it encapsulates. Below is the skeleton code for creating new fragment in Android.

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class MainActivity extends Fragment {

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  return inflater.inflate(R.layout.activity_main, container, false);
 }

}



Unlike Activities, Fragments need not be registered in your manifest. This is because fragments can exist only when embedded into an Activity, with their lifecycles dependent on that of the Activity to which they are added.

Adding Fragments to Activities
          Fragments can be added to our application by two methods as shown be based on the requirement.         
Method 1: Add Fragment to activity - For static fragments

        The simplest way to add a Fragment to an Activity is by including it within the Activities layout as shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment_name"
        android:name="com.fragment.example.Fragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

This technique works good when fragments are used to define static layouts based on various screens. If you wanted to go for dynamic layouts by adding, removing screens a better approach is to create layouts that use container views into which fragments can be placed at run-time as shown below:

Method 2: Add fragments to container views - For dynamic fragments

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Framelayout
        android:id="@+id/fragment_container"       
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

In this case we need to create and add the corresponding fragment to their parent containers within the onCreate handler of your Activity using Fragment Transactions.

Hope this gives a overview on what fragment means in Android, how to create fragments and to add them to your application/activity.

More tutorials on Fragments coming soon..






Reactions:

1 comment :

  1. waiting for the post , fragment is a just difficult concept for beginners mean while its give more interests to create app due to user interface.. post soon guys

    ReplyDelete