Simple Android Fragment Example

Simple android fragment example using code instead of xml for the layouts to keep things simpler.

Notes from: tutorial


import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {

            /* First we need to add a LinearLayout to act as a
            container for the rest of the UI and the Fragment in particular:
        LinearLayout linLayout = new LinearLayout(this);
        /*and this needs an id property so that we can store the
        Fragement's UI inside it. You can simply assign an id using setId, it only has
        to be unique at the current level of the View hierarchy:
        /*Finally we can add the LinearLayout to the ContentView:*/

        if (savedInstanceState == null) {

            MyFragment frag1 = new MyFragment();

        /*FragmentManager which is used to control how Fragments are displayed.
        To display a Fragment you first have to obtain the FragmentManager
        using getFragmentManager. Then you have to get a FragmentTransaction
        from the FragmentManager.
        /* You can't do anything with a Fragment unless you start a transaction.
        Within the transaction you can set up what you want to happen, usually
        add the Fragment to the current layout, but nothing happens until you
        use the commit method.
        /*The final complication is that the add method, used to add a
        Fragment to a specified container needs the container specified
         by id number.
        /* That is if you have a FragmentTransaction ft then:
                adds Fragment frag1 to the ViewGroup with the specified id. Notice this is not a Layout Id.

        /*Now we need to add the Fragment.
        To do this we create an instance of the Fragment, get the FragmentManager and use it
        (frag1 object)to get a FragmentTransaction object:
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            ft.add(100, frag1);




import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyFragment extends Fragment {

    /* onCreateView is the key method in creating a fragment,
     * it returns a single View object, a VIEW GROUP, with the
      * set of View Objects that define the Fragments UI.
      * The activity calls this view object when its time for the
      * fragment to provide its UI for display .
      * The activity passes a -
      * LayoutInflator : helps the Fragment create a View hierarchy from an XML file
      * a container : the ViewGroup that will contain the fragment's UI
      * A bundle: if the fragment already exists and has been suspended (much like an activity)
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /* here we're creating a view object in code, as opposed to XML
        to keep things clear
        We use getActivity() to return the Activity that the Fragment is associated with
        and this is what we use as the context when we create the View object within a fragment
        LinearLayout linLayout= new LinearLayout(getActivity());
        Button b = new Button(getActivity());
        b.setText("Hello Button");

        // create a text view in this fragment
        final TextView tv=new TextView(getActivity());
        tv.setText("Text Entry");

        // create an on click listener in this fragment
        View.OnClickListener onclick=
                new View.OnClickListener(){
                    public void onClick(View view){
                        Button bt=(Button) view;

        /* we now have a Fragment that returns a LinearLayout which contains a Button object
        with the text "Hello Button" on it
        return linLayout;


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Leave a Reply