Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Fragments — Add Fragments Dynamically At Runtime.

Real power of Frag­ments comes to the action when at the run­time your pro­gram decides which Frag­ment to dis­play based on the screen ori­en­ta­tion. Many a times you need a sce­nario when you want to dis­play dif­fer­ent user inter­face when screen changes its ori­en­ta­tion from por­trait to land­scape. We can cus­tomize the UI at the run­time using Frag­ments. Say for exam­ple you activ­ity looks good and fills the entire screen when opened in mobile but when opened in tablet which has the big­ger screen size than phones then your activ­ity doesn’t fill the screen and may appear less attrac­tive, in these cases you can dis­play more con­tent but manip­u­lat­ing with the Fragments.

How Would It Work:

    • First thing you need Frag­ment­Man­ager, Now using Frag­ment­Man­ager instance get Frag­ment­Trans­ac­tion instance.

  • Using Frag­ment­Trans­ac­tion instance you can per­form frag­ment trans­ac­tions which means you can add, remove and replace the fragments.
  • In the exam­ple below Frag­ment changes when device changes it ori­en­ta­tion. say we want to dis­play Fragment1 when device is in por­trait mode and Fragment2 when device is in the land­scape mode.
  • So now we just need to know the ori­en­ta­tion of the device. That you can get by int dis­play­mode = getResources().getConfiguration().orientation;
  • if dis­play­mode =1 then device is in por­trait mode else it’s in land­scape mode.

Fragment1.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:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment ONE"
        android:gravity="center"
        android:background="#5eff6a"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Fragment2.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:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment TWO"
        android:gravity="center"
        android:background="#ff9e5e"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"    >
</LinearLayout>

Fragment1.java

package com.example.dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, vg, false);
	}
}

Fragment2.java

package com.example.dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, vg, false);
	}
}

MainActivity.java

package com.example.dynamicfragments;

import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
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);
		setContentView(R.layout.main);

		FragmentManager fm = getFragmentManager();
		FragmentTransaction fragmentTransaction = fm.beginTransaction();

		// get the display mode
		int displaymode = getResources().getConfiguration().orientation;
		if (displaymode == 1) { // it portrait mode
			Fragment1 f1 = new Fragment1();
			fragmentTransaction.replace(android.R.id.content, f1);
		} else {// its landscape
			Fragment2 f2 = new Fragment2();
			fragmentTransaction.replace(android.R.id.content, f2);
		}
		fragmentTransaction.commit();

	}
}

Por­trait Mode:

 

Dynamic Fragments - Portrait Mode

Dynamic Frag­ments — Por­trait Mode

Land­scape Mode:

Dynamic Fragments - Landscape Mode

Dynamic Frag­ments — Land­scape Mode

You may also like...

Leave a Reply

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