Android. How to add static Fragment to Activity?

In this tutorial, I will demonstrate how to add fragment to activity in static way. First of all, create new Android application project. Our activity will contain 2 fragments. Each fragment will refer to layout. First of all, we will create layout for first fragment.

fragment_one.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="wrap_content"
       android:layout_height="wrap_content"
       android:text="Fragment One"
       android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Now we will create Fragment class for first fragment. From this class we will refer to fragment_one.xml layout.
FragmentOne.java

package net.joerichard.staticfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, null);
    }
   
}

Let’s create layout for second fragment as we did for the first fragment
fragment_two.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="wrap_content"
       android:layout_height="wrap_content"
       android:text="Fragment Two"
       android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Now add second Fragment class
FragmentTwo.java

package net.joerichard.staticfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_two, null);
    }
}

Now open activity_main.xml and add two fragments. In order to do this, open Graphical representation for layout. From Palette open Layouts tab. Drag and drop 2 fragments. When you put fragments, Eclipse will ask you which fragment you want to show. Select corresponding fragments: FragmentOne and FragmentTwo.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
       android:id="@+id/fragment1"
       android:name="net.joerichard.staticfragment.FragmentOne"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"/>

    <fragment
       android:id="@+id/fragment2"
       android:name="net.joerichard.staticfragment.FragmentTwo"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1" />

</LinearLayout>

MainActivity.java

package net.joerichard.staticfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

That’s it. Run application. Now you can see 2 fragments which contains 2 layouts.
Screenshot_2014-08-29-09-37-04
Download Source Code