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

In our earlier post we have seen “What is Fragments?”. and I said there are two ways to add Fragments to an Activity, either static fragments or Dynamic fragments.

In this article we will see how to add Static Fragments to an Activity, by Declare the Fragment as <fragment> a element inside the activity’s layout file.

Steps:

  • Create MainActivity .java file and extends Activity.
  • Create Fragment1.java and Fragment2.java which extends the Fragment Class.
  • Create the respective layouts for Fragment1 and Fragment2.
  • Override onCreate() method in Fragment1.java and Fragment2.java, and inflate the Fragment layouts ( R.id.Fragment1 and R.id.Fragment2) using LayoutInflater Class object and by passing the ViewGroup argument which is the activity in which the fragment will be embedded.

Complete Example:

Fragment1.xml

[sourcecode language=”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>

[/sourcecode]

Fragment2.xml

[sourcecode language=”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>

[/sourcecode]

activity_main.xml

[sourcecode language=”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>

[/sourcecode]

Fragment1.java

[sourcecode language=”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);

}
}

[/sourcecode]

Fragment2.java

[sourcecode language=”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);

}
}

[/sourcecode]

Static Fragments 1 Static Fragments 2

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

Leave a Reply

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

Show Buttons
Hide Buttons