Adding Fragments Dynamically In Android Application

I have discussed how to add a fragment statically in my last article. To start the development of Android Applications, some useful links are given below:
  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications
  8. Fragments In Android Applications


In this article, you will see how to add a fragment dynamically at run time. We can compartmentalize our UI into the various configurable parts, using a fragment. But the real power of fragments is realized when you add them dynamically to activities during runtime. In the article Fragments In Android Applications, you have seen, how you can add the fragments to activity by modifying XML files during design time. It is more useful when you create the fragments and add them to the activities during runtime.
This enables you to create a customizable user interface for your application. E.g. if the Application is running on a smartphone, you might fill an activity with a single fragment, but if the same application is running on a tablet, you might fill the activity with two or more fragments, as the tablet has more screen size, compared to a smartphone.
  1. package com.example.administrator.myfragmentapp;    
  2. import;    
  3. import;    
  4. import;    
  5. import android.os.Bundle;    
  6. import android.view.Display;    
  7. import android.view.Menu;    
  8. import android.view.MenuItem;    
  9. import android.view.WindowManager;    
  10. public class MainActivity extends ActionBarActivity     
  11. {    
  12.     @Override    
  13.     protected void onCreate(Bundle savedInstanceState)     
  14.     {    
  15.         super.onCreate(savedInstanceState);    
  16.         //setContentView(R.layout.activity_main);    
  17.         FragmentManager fragmentManager = getFragmentManager();    
  18.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();    
  19.         //get the current device information    
  20.         WindowManager windowManager = getWindowManager();    
  21.         Display display = windowManager.getDefaultDisplay();    
  22.         if (display.getWidth() > display.getHeight()) {    
  23.             //This is landscape mode    
  24.             Fragment1Activity fragment1Activity1 = new Fragment1Activity();    
  25.             // refers to the content view of the activity    
  26.             fragmentTransaction.replace(, fragment1Activity1);    
  27.         } else {    
  28.             //This is portrait mode    
  29.             Fragment2Activity fragment2Activity = new Fragment2Activity();    
  30.             fragmentTransaction.replace(, fragment2Activity);    
  31.         }    
  32.         fragmentTransaction.commit();    
  33.     }    
  34.     @Override    
  35.     public boolean onCreateOptionsMenu(Menu menu) {    
  36.         // Inflate the menu; this adds items to the action bar if it is present.    
  37.         getMenuInflater().inflate(, menu);    
  38.         return true;    
  39.     }    
  40.     @Override    
  41.     public boolean onOptionsItemSelected(MenuItem item) {    
  42.         // Handle action bar item clicks here. The action bar will    
  43.         // automatically handle clicks on the Home/Up button, so long    
  44.         // as you specify a parent activity in AndroidManifest.xml.    
  45.         int id = item.getItemId();    
  46.         //noinspection SimplifiableIfStatement    
  47.         if (id == {    
  48.             return true;    
  49.         }    
  50.         return super.onOptionsItemSelected(item);    
  51.     }    
  52. }  
Run the Application by clicking F11 on the Android emulator. By default, the Android emulator is in the portrait mode.
Now, change the orientation of the emulator from the portrait to the landscape by clicking the button <Ctrl> + F11.
To add the fragments to an activity, you use the FragmentManager class by first obtaining an instance of it.
  1. FragmentManager fragmentManager=getFragmentManager();  
FragmentTransaction class is used to perform the fragment transaction in your activity for either adding, removing or replacing.
  1. FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();   
In the code, given above, WindowManager is used to determine whether the device in currently in the portrait mode or landscape mode.
Once, it is determined, you add the appropriate fragment to the activity by creating the fragment and after the call, the replace() method of the FragmentTransaction object needs to be used. This adds the fragment to the specified view container.
Here, refers to the content view of the activity.
  1. //This is landscape mode  
  2. Fragment1Activity fragment1Activity1 = new Fragment1Activity();  
  3. // refers to the content view of the activity  
  4. fragmentTransaction.replace(, fragment1Activity1);  
Using the replace() method is essentially the same as calling the remove() method, followed by the add() method of the FragmentTransaction object. To ensure the changes take effect, you need to call the commit() method.


In the above article, we saw, how to add the fragment dynamically. In the next article, I will explain about the complete life cycle of a fragment. 

Similar Articles