Add Fragments Dynamically
Description
The following code shows how fragments can be added programmatically to an activity during runtime.
Example
Modify the activity_main.xml
file as follows:
<?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="horizontal" >
</LinearLayout>/*from w w w . j a v a 2 s. c om*/
In the res/layout
folder, add a new file and name it fragment1.xml
.
Populate it with the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout/* ww w .ja v a2 s. c o m*/
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Also in the res/layout
folder, add another new file and name it fragment2.xml
.
Populate it as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout/*from ww w .j ava 2s . c o m*/
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFE00"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is fragment #2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
Under the com.java2s.Fragments
package name, add two
Java class files and name them Fragment1.java
and Fragment2.java
Add the following code to Fragment1.java
:
package com.java2s.myapplication3.app;
// w w w. jav a 2 s.c o m
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 {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Add the following code to Fragment2.java
:
package com.java2s.myapplication3.app;
/*from w ww.ja v a2 s. c o m*/
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 {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment2, container, false);
}
}
Add the following code to the MainActivity.java
file:
package com.java2s.myapplication3.app;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
/* w ww. ja v a2 s. c o m*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
//get the current display info
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
if (d.getWidth() > d.getHeight())
{
//landscape mode
Fragment1 fragment1 = new Fragment1();
// android.R.id.content refers to the content
// view of the activity
fragmentTransaction.replace(
android.R.id.content, fragment1);
}
else
{
//portrait mode
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(
android.R.id.content, fragment2);
}
fragmentTransaction.commit();
}
}
Note
Run the application on the Android emulator. Observe changes when switch from portrait mode to landscape mode.