Basics of Fragments Using Android Studio 1.0

Introduction

A Fragment is a modular section of an activity that has its own lifecycle, receives its own input events and which you can add or remove while the activity is running. For example it could be thought of as a "sub activity" that you can reuse in multiple activities - Google.

Here we are just extending the Fragment class to the lower versions say 1.6 using the support library. This could be done using Android Studio 1.0 however we can do this using any other IDE. But here I will use Android Studio 1.0.

To extend the Fragment class use the Support Library to make an application compatible with devices running on Android versions as low as Android 1.6. For this use the procedure given below.

Support Library Setup

To use the support library in the lower versions say 1.6 of Android first you need to make it ready for the implementation. Let us use the procedure given below to use a support library for creating the Fragment class in lower versions.

Step 1

Start the Android SDK manager.

toolbar android studio

Just click on the red portion or tab. Then a SDK manager window appears. 

Step 2

In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and if necessary, expand to show its contents.

sdk manager

Step 3

Select the Android Support Library and Android Support Repository and Install them.

However I have Installed them when Android Studio was installed in my PC but there is no need to worry, the procedure is clearly specified above. After downloading the Support library to your application then use the following procedure.

Adding the support library

After following the preceding procedure now we need to understand how to add the commands or lines of code in the Fragment class. To use a Support Library, we must modify the application's project's classpath dependencies within the development environment.

Some Support Libraries contain resources beyond compiled code classes, such as images or XML files. For example, the" v7 appcompat" and "v7 gridlayout" libraries include resources.

How to add the support libraries

  • Without Resource

    In the Android studio there is a different procedure because of Gradle in the build.gradle file. All the dependencies must be put here. Now add the Android support repository here with the following line of code.

    1. dependencies {
    2. ...
    3. compile "com.android.support:support-v4:18.0.+"
    4. }

    However this plus signifies the use of the version higher than this.

  • With Resource

    In the Android Studio as said previously there is a different scenario. Here now add code to the dependencies in Android Studio 1.0 as shown below.

    dependencies

Creating the Fragment Class

To create the Class of this type we must extend the Fragment class then must override the lifecycle methods. Let us have a look at the following code. Use the onCreateView() callback method to define the layout. Basically this is the only callback method we need to get a fragment running.

  1. package com.example.gkumar.fragmentactivity;  
  2. import android.app.Fragment;  
  3. import android.support.v7.app.ActionBarActivity;  
  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. public class FRAGActivity extends Fragment {  
  11. @Override  
  12.    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {  
  13.    // Inflate the layout for this fragment  
  14.       return inflater.inflate(R.layout.FRAGActivit_view, container, false);  
  15. }  
  16. }  

 

Just like an activity, a fragment should implement other lifecycle callbacks that allow programmers to manage its state since it is added or removed from the activity and as the activity transitions between its lifecycle states. For example, when the activity's onPause() method is called, any fragments in the activity also receive a call to the onPause() method.

Adding a Fragment to an Activity using XML

While fragments are reusable, modular user interface components, each instance of a Fragment class must be associated with a parent FragmentActivity. You can do this association by defining each fragment within your activity layout XML file.

Note: FragmentActivity having ActionBarActivity as a subclass is a special activity provided in the Support Library to handle fragments on system versions older than API level 11.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.   
  7.     <fragment android:name="com.example.gkumar.fragmentactivity"  
  8.         android:id="@+id/headlines_fragment"  
  9.         android:layout_weight="1"  
  10.         android:layout_width="0dp"  
  11.         android:layout_height="match_parent" />  
  12.   
  13.     <fragment android:name="com.example.gkumar.fragmentactivity"  
  14.         android:id="@+id/article_fragment"  
  15.         android:layout_weight="2"  
  16.         android:layout_width="0dp"  
  17.         android:layout_height="match_parent" />  
  18.   
  19.     <TextView android:text="@string/hello_world"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content" />  
  22.   
  23. </LinearLayout>  
Then apply the layout to the activity file FRAGActivity.java
  1. import android.os.Bundle;  
  2. import android.support.v4.app.FragmentActivity;  
  3.   
  4. public class FRAGActivity extends FragmentActivity {  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.news_articles);  
  9.     }  
  10. }  
Note: If you are using the v7 appcompat library, your activity should instead extend ActionBarActivity that is a subclass of FragmentActivity

Summary

This article explained how to create Fragments in Android using the Android Studio 1.0. All these classes and methods depend upon the target and the minimum SDK version we are using here. We used the minimum SDK version.

Up Next
    Ebook Download
    View all
    Learn
    View all