In this lab, you will work with Android Fragments. The application that you create for this lab will not do much, if any processing. Rather, it shows you the event lifecycle of adding and replacing fragments and how fragments are displayed within their container.
For this lab, I have posted the document electronically. I suggest that you load the Microsoft Word document. That way you can copy and paste my code into your lab. There is a significant amount of code in this program. For each code segment that you develop, there is a discussion of the code you have written. In some cases, you will modify the code on your own.
For this application, you will need to create 5 layouts. Two of these layouts are used to render the activity while in portrait and landscape mode. The following list describes the activities.
The first layout is named activity_main.xml and appears in the layout folder. This layout will be rendered while the device is in portrait mode. This activity contains two buttons and two fragments that will be rendered vertically
The second layout is named activity_main.xml and appears in the layout-land folder. This layout will be rendered while the device is in landscape mode. The activity contains two buttons and two fragments that will be rendered horizontally.
There are three fragments. Each works the same way and will be rendered inside one of the parent activities.
The first fragment named fragment_one_layout.xml contains one TextView used to illustrate the activity and fragment events.
The second fragment named fragment_two_layout.xml contains one TextView used to illustrate the activity and fragment events.
The third fragment named fragment_three_layout.xml contains one TextView used to illustrate the activity and fragment events.
The following excerpt from the Package Explorer shows the files and their folder location.
Create a new Android Application project as you have been doing. I suggest using version 19 of the API’s. Create the application with a blank activity. Use the default name for the main activity. So that all of the names match, I suggest that you name the application (package) fragmentdemo1.
Edit the file named activity_main.xml in the res/layout folder. Enter the following code:
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/replace_fragment" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add_fragment" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/fragment_one_holder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#6495ED" />
android:id="@+id/fragment_two_holder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#40E0D0" />
Note the following about the preceding statements.
There are two nested elements. The first contains two buttons appearing horizontally. The second contains two s organized vertically. These elements will render one of the three fragments:
Modify the strings.xml file appearing in the values folder adding the following lines (shown in bold).
Next you will create a second activity. This activity will be displayed while the device is oriented in landscape mode.
Create a folder named layout-land in the res folder. Remember that this name is magical based on the default configuration. This activity will be rendered when the device is in landscape mode.
Create a file named activity_main.xml in this folder and copy the following code into the file.
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
android:id="@+id/fragment_one_holder"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#6495ED" />
android:id="@+id/fragment_two_holder"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#40E0D0" />
HANDS-ON ACTIVITY: Creating the Fragment Layouts
Next you will create the layout for the three fragment.
Create a fragment named fragment_one_layout.xml in the layout folder. Enter the following code.
* Overriding onBackPressed button and pop backStack fragment.
*
**************************************/
@Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
}
The following list identifies significant points about the preceding code.
The two variables named FRAGMENT_ONE_POSITION and FRAGMENT_TWO_POSITION indicate whether the first, second, or third fragment in one of the two frames.
An onClick listener is set up for each of the two buttons. The first replaces fragments, the second adds them. The purpose of this is to demonstrate the operation of the back stack. You will see this behavior at the end of the exercise, when you run the application.
First, the fragment transaction is created.
The switch statement tests the current position, which is initially 0.
Each of the case statements replace the current fragment with the new one.