Overview
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:
- Android Programming - Day One
- Android Programming - Day Two
- How To Display A Dialog Windows In Android
- How To Display A Progress Dialog Window In Android
- Intent In Android
- Return Data Using Intent Object In Android Applications
- Passing Data Using An Intent Object In Android Applications
- Fragments In Android Applications
Introduction
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.
Code
- package com.example.administrator.myfragmentapp;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Display;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.WindowManager;
- public class MainActivity extends ActionBarActivity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
-
- WindowManager windowManager = getWindowManager();
- Display display = windowManager.getDefaultDisplay();
- if (display.getWidth() > display.getHeight()) {
-
- Fragment1Activity fragment1Activity1 = new Fragment1Activity();
-
- fragmentTransaction.replace(android.R.id.content, fragment1Activity1);
- } else {
-
- Fragment2Activity fragment2Activity = new Fragment2Activity();
- fragmentTransaction.replace(android.R.id.content, fragment2Activity);
- }
- fragmentTransaction.commit();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Result
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.
Explanation
To add the fragments to an activity, you use the FragmentManager class by first obtaining an instance of it.
- FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction class is used to perform the fragment transaction in your activity for either adding, removing or replacing.
- 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, Android.R.id.content refers to the content view of the activity.
-
- Fragment1Activity fragment1Activity1 = new Fragment1Activity();
-
- fragmentTransaction.replace(android.R.id.content, 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.
fragmentTransaction.commit();
Conclusion
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.