Add Layout in Fragments In Android

Introduction

 
This article explains how to add a Layout to a Fragment.
 
Fragments
 
Fragments are more used for the user interface benefit. Fragments are used when the user wants to see two different views of two different classes on the same screen. Fragments were added to Android when Honeycomb was launched. So if you are developing an application only for Android 3.0 (HoneyComb) or higher then Android provides you access to the Fragments class. You can also access the FragmentManager by calling the getFragmnetManager() method in your Activities.
 
However, most applications are being developed for Android 2.1 Eclairs because Android Eclairs cover most of the market. So if you are developing applications for Android 2.1 Eclairs then you need to install the Android compatibility library to use Fragments. When you use the Android Compatibility library you need to extend FragmentActivity to your class to use Fragments. You can get the FragmentManager by calling the getSupportFragmentManager() method. Otherwise, all the details will be the same as for Honeycomb.
 
For creating Fragments your class must extend the Fragment class. The Fragment class has methods, just the same as that of an Activity, like onStart(), onPause, onResume() and onCreate(). Usually you should use the onCreateVIew(), onCreate() and onStop() methods in your class.
  • oncreate(): The system calls this method when the Fragment is created.
  • onCreateView(): The system calls this method when Android needs the Layout for the Fragment.
  • onPause(): The system calls this method when the user leaves the Fragment. But it does not mean that the Fragment will be destroyed.
Most applications apply this method to use Fragments in its application. But you can use various methods depending on your needs.
 
Step 1
 
Create a project like this:
 
fragmetsproject
 
Step 2
 
Create an XML file with the following.
 
In this, you will use a Fragment to hold the Layout in an Activity. In the android:name="com.fragmentactivityproject.Second" property you will that class with a package name that you want to show on the activity at run time.
  1. <RelativeLayout 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.     tools:context=".MainActivity"  
  6.     android:background="#ffab52">  
  7.    
  8.     <fragment  
  9.         android:layout_width="150dp"  
  10.         android:layout_height="wrap_content"  
  11.         android:name="com.fragmentactivityproject.Second"  
  12.         android:id="@+id/fragment"  
  13.         android:layout_centerInParent="true"  
  14.         tools:layout="@layout/activity_main" />  
  15.    
  16. </RelativeLayout> 
Step 3
 
Create another XML file with the following.
 
In this Activity I have used three buttons, you can create your Layout depending on your needs.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:background="#000000"  
  7.     android:layout_height="fill_parent">  
  8.    
  9.     <Button  
  10.         android:text="Button1"  
  11.         android:textStyle="bold"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_width="wrap_content"  
  14.         android:textSize="30dp"  
  15.         android:ems="8"  
  16.         android:id="@+id/button1"  
  17.         android:textColor="#ffab52"  
  18.         android:layout_gravity="center_horizontal"/>  
  19.    
  20.     <Button  
  21.    
  22.         android:id="@+id/Button2"  
  23.         android:text="Button2"  
  24.         android:textStyle="bold"  
  25.         android:ems="8"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_width="wrap_content"  
  28.         android:textSize="30dp"  
  29.         android:textColor="#ffab52"  
  30.         android:layout_gravity="center_horizontal"/>  
  31.     <Button  
  32.    
  33.         android:id="@+id/Button3"  
  34.         android:text="Button3"  
  35.         android:textStyle="bold"  
  36.         android:ems="8"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_width="wrap_content"  
  39.         android:textSize="30dp"  
  40.         android:textColor="#ffab52"  
  41.         android:layout_gravity="center_horizontal"/>  
  42. </LinearLayout> 
Step 4
 
Create a Java class file with the following.
 
This is your rmainActivity class that will hold your Layout at runtime.
  1. package com.fragmentactivityproject;  
  2.    
  3. import android.annotation.TargetApi;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Build;  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.support.v4.app.FragmentActivity;  
  10. import android.view.Menu;  
  11.    
  12. public class MainActivity extends FragmentActivity {  
  13.    
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.    
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.     }  
  20.    
  21.     @Override  
  22.     public boolean onCreateOptionsMenu(Menu menu) {  
  23.         // Inflate the menu; this adds items to the action bar if it is present.  
  24.         getMenuInflater().inflate(R.menu.main, menu);  
  25.         return true;  
  26.     }   
Step 5
 
Create another Java class file with the following.
  1. package com.fragmentactivityproject;  
  2.    
  3. import android.annotation.TargetApi;  
  4. import android.app.FragmentTransaction;  
  5. import android.support.v4.app.Fragment;  
  6. import android.os.Build;  
  7. import android.os.Bundle;  
  8. import android.support.v4.app.FragmentManager;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.Button;  
  14.    
  15. import static com.fragmentactivityproject.R.layout.second;  
  16.    
  17. /** 
  18.  * Created by vivek on 11/14/13. 
  19.  */  
  20. @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  21. public class Second extends Fragment {  
  22.    
  23.     Button button1;  
  24.     public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)  
  25.     {  
  26.         View view;  
  27.         view = inflater.inflate(R.layout.second,null);  
  28.         return view;  
  29.     }  
Step 6
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.fragmentactivityproject"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.fragmentactivityproject.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 7
 
Image that contains your Layout:
 
fragmetslayout


Similar Articles