Easy Tutorial
❮ Android Environment Setup Android Eclipse ❯

Android List Fragment

The basic implementation of a list fragment is used to create a list of items within a fragment.


Example

This example explains how to create a list fragment based on ArrayAdapter. Let's start with the following steps:

Step Description
1 Create an Android application using Android Studio, named List Fragment, with package name cn.uprogrammer.listfragment
2 Modify the string file, add new string constants in res/values/string.xml
3 Create a layout file named list_fragment.xml under res/layout to define the list fragment, and add a fragment tag in activity_main.xml
4 Create MyListFragment.java file, which includes onCreateView(), onActivityCreated(), and OnItemClickListener() methods.
5 Start the Android emulator to run the application and verify the changes made.

Before starting coding, initialize string constants in the string.xml file under the res/values directory.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">listfragment</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_main">List Fragment Demo</string>
    <string name="imgdesc">imgdesc</string>

    &lt;string-array name="Planets">
        <item>Sun</item>
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>
</resources>

Below is the content of the res/layout/activity_main.xml file, which includes a LinearLayout and a fragment tag.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/fragment1"
        android:name="cn.uprogrammer.listfragment.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Below is the content of the res/layout/list_fragment.xml file, which includes a LinearLayout, ListView, and TextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>
</LinearLayout>

Below is the content of the src/cn.uprogrammer.listfragment/MyListFragment.java file. Before starting coding, follow these steps:

Below is the content of MainActivity.java:

package cn.uprogrammer.listfragment;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Below is the content of the AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.uprogrammer.listfragment">

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

Let's run the modified List Fragment application. I assume you have already created an AVD when setting up the environment. Open the activity file in your project and click on the run icon in the toolbar.

❮ Android Environment Setup Android Eclipse ❯