Saturday, April 28, 2012

Inflate Fragments Layouts in Android



Fragment

You can see the previous post "Working with Fragments in Android" to know about Fragment. Let's directly move on example.

Example

- The example illustrates to inflate layout(.xml) file in Fragment area.
Step - 1:
- Create arrays.xml in /res/values and create string array as <string-array name="android_platforms">.
- Add below items in string array and it will look like as below
    <string-array name="android_platforms">
        <item>Android 3.2</item>
        <item>Android 3.1</item>
        <item>Android 3.0</item>
        <item>Android 2.3.4</item>
        <item>Android 2.3.3</item>
        <item>Android 2.2</item>
        <item>Android 2.1</item>
        <item>Android 2.3</item>
        <item>Android 2.0.1</item>
        <item>Android 2.0</item>
        <item>Android 1.6</item>
        <item>Android 1.5</item>
        <item>Android 1.1</item>
    </string-array>
- Create .xml files in /res/layout as per your requirement.
Step - 2:
- Get the position of selected list item.
    int position = getArguments().getInt("position", 0);
- Here, we are inflating layouts in onCreateView(...). The layout will change based on the position of list item.
    View view = null;
    if(position == 0) {
        view = inflater.inflate(R.layout.fr_layout_1, null);
    } else if(position == 1) {
        view = inflater.inflate(R.layout.fr_layout_2, null);
    } else if(position == 2) {
        view = inflater.inflate(R.layout.fr_layout_3, null);
    } else if(position == 3) {
        view = inflater.inflate(R.layout.fr_layout_4, null);
    } else if(position == 4) {
        view = inflater.inflate(R.layout.fr_layout_5, null);
    } else if(position == 5) {
        view = inflater.inflate(R.layout.fr_layout_6, null);
    } else if(position == 6) {
        view = inflater.inflate(R.layout.fr_layout_7, null);
    } else if(position == 7) {
        view = inflater.inflate(R.layout.fr_layout_8, null);
    } else if(position == 8) {
        view = inflater.inflate(R.layout.fr_layout_9, null);
    } else if(position == 9) {
        view = inflater.inflate(R.layout.fr_layout_10, null);
    } else if(position == 10) {
        view = inflater.inflate(R.layout.fr_layout_11, null);
    } else if(position == 11) {
        view = inflater.inflate(R.layout.fr_layout_12, null);
    } else if(position == 12) {
        view = inflater.inflate(R.layout.fr_layout_13, null);
    }
Step - 3:
- Follow this step from the older article "Working with Fragments in Android".
Step - 4:
- Create main_fragment.xml in /res/layout.
- Register FragmentListTitles in <fragment /> tag.
    <fragment
        android:id = "@+id/fragment1"
        android:name = "com.indianic.fragmentview.FragmentListTitles"
        android:layout_width = "0dp"
        android:layout_height = "match_parent"
        android:layout_weight = "1"
        android:layout_marginRight = "4dp">
    </fragment>
The android:name attribute has below format <package name>.<class name>
android:name = "com.indianic.fragmentdemo.FragmentListTitles"
- Set a FrameLayout to display content.
    <FrameLayout
         android:id = "@+id/fragment2"
         android:layout_weight = "1"
         android:layout_width = "0px"
         android:layout_height = "match_parent"
         android:background = "?android:attr/detailsElementBackground">
    </FrameLayout>
Step - 5:
- Run application.
Find the attached zip file for complete source code.
I would be glad to receive your suggestions regarding it.
Thanks.

No comments:

Post a Comment