A ListFragment Application with backward compatibility in Android

July 2, 2012
By

ListFragment is a fragment class available in the package android.app and it is used to list a set of items in as a listview. Fragment is a new concept which is introduced in Android 3.0 and its higher versions. But we can create applications having fragments for the prior versions of Android till 1.6 using an additional support library. In this article we will create a ListFragment application using this additional support library for Android 2.3.


1. Create an Android Project namely “ListFragmentWithBackward”

 

New Android Project

Figure 1 : New Android Project


2. Select Android Build target 2.3

Select Build Target

Figure 2 : Select Build Target


3. Enter application details

 

Application Details

Figure 3: Application Details


4. Add fragment support to the application

Right click project explorer and then click the menu Android Tools -> Add Support library


5. src/in/wptrafficanalyzer/listfragmentwithbackward/CountryList.java


package in.wptrafficanalyzer.listfragmentwithbackward;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CountryList extends ListFragment {

    /**
    * The list of countries to be shown in the ListFragment
    */
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /**
        * Creating an array adapter to host the list of items to be displayed in the ListFragment
        */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

  • CountryList class inherits ListFragment class and sets the items list to it using ArrayAdapter

6. res/layout/main.xml


<?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="vertical" >

    <fragment
        android:tag="country_fragment"
        android:name="in.wptrafficanalyzer.listfragmentwithbackward.CountryList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</LinearLayout>


7. src/in/wptrafficanalyzer/listfragmentwithbackward/MainActivity.java


package in.wptrafficanalyzer.listfragmentwithbackward;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


8. Application in execution

 

Application in execution

Figure 4 : Application in execution


9. Download


10. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

2 Responses to A ListFragment Application with backward compatibility in Android

  1. santhosh on February 27, 2013 at 2:36 pm

    Hi, Great Tutorial. Thanks. I have a small issue. How can I show first item details automatically in the second fragment when the device is in landscape mode??

  2. Thesalimkip on August 26, 2013 at 12:32 pm

    How do you implement onclicklistitem when a list item is clicked? Am having a problem implementing.Thanks

Leave a Reply to Thesalimkip Cancel reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner