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”
2. Select Android Build target 2.3
3. Enter 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
9. Download

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

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
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??
How do you implement onclicklistitem when a list item is clicked? Am having a problem implementing.Thanks