Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Static Fragments — Declare the Fragment as a element inside the activity’s layout file.

In our ear­lier post we have seen “What is Frag­ments?”. and I said there are two ways to add Frag­ments to an Activ­ity, either sta­tic frag­ments or Dynamic fragments.

In this arti­cle we will see how to add Sta­tic Frag­ments to an Activ­ity, by Declare the Frag­ment as <frag­ment> a ele­ment inside the activity’s lay­out file.

Steps:

  • Cre­ate Main­Ac­tiv­ity .java file and extends Activ­ity.
  • Cre­ate Fragment1.java and Fragment2.java which extends the Frag­ment Class.
  • Cre­ate the respec­tive lay­outs for Fragment1 and Fragment2.
  • Over­ride onCre­ate() method in Fragment1.java and Fragment2.java, and inflate the Frag­ment lay­outs ( R.id.Fragment1 and R.id.Fragment2) using Lay­outIn­flater Class object and by pass­ing the View­Group argu­ment which is the activ­ity in which the frag­ment will be embedded.

Com­plete Example:

Fragment1.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment ONE"
        android:gravity="center"
        android:background="#5eff6a"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Fragment2.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment TWO"
        android:gravity="center"
        android:background="#ff9e5e"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"    >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.staticfragments.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.staticfragments.Fragment2"
        android:layout_marginLeft="5sp"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>

Fragment1.java

package com.example.staticfragments;

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

@SuppressLint("NewApi")
public class Fragment1 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment1, vg,false);

	}
}

Fragment2.java

package com.example.staticfragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment2, vg,false);

	}
}

Static Fragments 1 Static Fragments 2

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *