Simple android fragment example using code instead of xml for the layouts to keep things simpler.
Notes from: http://www.i-programmer.info/programming/android/6882-introducing-android-fragments.html tutorial
MainActivity.java
package com.jamesfroggatt.androidfragments.app; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.widget.LinearLayout; public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(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: */ linLayout.setId(100); /*Finally we can add the LinearLayout to the ContentView:*/ setContentView(linLayout); 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: ft.add(id,frag1); 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); ft.commit(); } } }
MyFragment.java
package com.jamesfroggatt.androidfragments.app; import android.os.Bundle; import android.support.v4.app.Fragment; 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) */ @Override 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"); linLayout.addView(b); // create a text view in this fragment final TextView tv=new TextView(getActivity()); tv.setText("Text Entry"); linLayout.addView(tv); // create an on click listener in this fragment View.OnClickListener onclick= new View.OnClickListener(){ @Override public void onClick(View view){ Button bt=(Button) view; tv.setText(bt.getText()); } }; b.setOnClickListener(onclick); /* we now have a Fragment that returns a LinearLayout which contains a Button object with the text "Hello Button" on it */ return linLayout; } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jamesfroggatt.androidfragments.app" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.jamesfroggatt.androidfragments.app.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>