Dynamically add Fragments to an Activity in Android

June 30, 2012

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”

New Android Project

Figure 1: New Android Project

2. Select Android build target

Select Android Build Target

Figure 2: Select Android Build Target

3. Enter application details


Application Details

Figure 3 : Application Details

4. res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>

    <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>


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:orientation="vertical" >



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 {
    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:orientation="vertical" >



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. */
    public void onCreate(Bundle savedInstanceState) {

        Button btnLoad = (Button) findViewById(R.id.btn_load);

        OnClickListener listener = new OnClickListener() {

            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                HelloFragment hello = new HelloFragment();
                fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");



9. Execute the application


Main Activity

Figure 4 : Main Activity

  • On clicking the “Load Fragment” button of the Figure 4, a fragment will be loaded as shown in the Figure 5.
Main Activity with a dynamically added fragment

Figure 5: Main Activity with a dynamically added fragment

10. Download

11. Reference


How to hire me?

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

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , ,

8 Responses to Dynamically add Fragments to an Activity in Android

  1. aisha on December 24, 2012 at 11:39 pm

    so great website thank you very much it’s so helpful :)

  2. GauravKumawat on January 4, 2013 at 11:38 am

    Once again a nice tutorial…:P george……….

  3. srujan on August 19, 2014 at 9:42 pm

    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?

  4. ruchika on September 22, 2015 at 12:59 pm

    Very Nice Post.So helpful.

  5. shital on October 12, 2015 at 5:16 pm

    very nice explanation.. i got it now…..

  6. peter on October 24, 2015 at 10:00 pm

    well thanks man it really workes…
    i have been searching this for all day… ;)

  7. Oscar David Díaz Fortaleché on February 5, 2016 at 3:06 am

    Thank you for keep it simple. Have a nice day.

  8. Kimsun on March 29, 2016 at 8:24 am

    Thanks! This really works!

Leave a Reply to GauravKumawat Cancel reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner