In this article , we will create an Android application in which a fragment can be added to an activity on a button click.
1. Create an Android project namely “DynamicFragments”
2. Select Android build target
3. Enter application details
4. res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">DynamicFragments</string> <string name="str_btn_load">Load Fragment</string> <string name="str_tv_fragment">This is a fragment</string> </resources>
5. res/layout/hello_fragment_layout.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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/str_tv_fragment" /> </LinearLayout>
6. src/in/wptrafficanalyzer/dynamicfragments/HelloFragment.java
package in.wptrafficanalayzer.dynamicfragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HelloFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** Inflating the layout for this fragment **/ View v = inflater.inflate(R.layout.hello_fragment_layout, null); return v; } }
7. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_load" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/str_btn_load" /> <LinearLayout android:id="@+id/fragment_container" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout>
8. src/in/wptrafficanalyzer/dynamicfragments/MainActivity.java
package in.wptrafficanalayzer.dynamicfragments; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnLoad = (Button) findViewById(R.id.btn_load); OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); HelloFragment hello = new HelloFragment(); fragmentTransaction.add(R.id.fragment_container, hello, "HELLO"); fragmentTransaction.commit(); } }; btnLoad.setOnClickListener(listener); } }
9. Execute the application
- On clicking the “Load Fragment” button of the Figure 4, a fragment will be loaded as shown in the Figure 5.
10. Download

11. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
so great website thank you very much it’s so helpful
Once again a nice tutorial…:P george……….
Thanks your source code helped me to findout the simple mistake I made in my application.
Instead of null in the below line
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
I gave container. Why is that null and when is it not null?
Very Nice Post.So helpful.
very nice explanation.. i got it now…..
well thanks man it really workes…
i have been searching this for all day…
Thank you for keep it simple. Have a nice day.
Thanks! This really works!