• Android Video Tutorials

Android - list Fragment

Static library support version of the framework's ListFragment. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used.

The basic implementation of list fragment is for creating list of items in fragments

List Fragment

List in Fragments


This example will explain you how to create your own list fragment based on arrayAdapter. So let's follow the following steps to similar to what we followed while creating Hello World Example −

Step Description
1 You will use Android Studio to create an Android application and name it as SimpleListFragment under a package com.example.tutorialspoint7.myapplication, with blank Activity.
2 Modify the string file, which has placed at res/values/string.xml to add new string constants
3 Create a layout called list_fragment.xml under the directory res/layout to define your list fragments. and add fragment tag(<fragment>) to your activity_main.xml
4 Create a myListFragment.java, which is placed at java/myListFragment.java and it contained onCreateView(),onActivityCreated() and OnItemClickListener()
5 Run the application to launch Android emulator and verify the result of the changes done in the application.

Before start coding i will initialize of the string constants inside string.xml file under res/values directory

<?xml version="1.0" encoding="utf-8"?>
   <string name="app_name">ListFragmentDemo</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="imgdesc">imgdesc</string>
   <string-array name="Planets">


Following will be the content of res/layout/activity_main.xml file. it contained linear layout and fragment tag.

<?xml version="1.0" encoding="utf-8"?>
   android:orientation="vertical" >
      android:layout_height="match_parent" />


Following will be the content of res/layout/list_fragment.xml file. it contained linear layout,list view and text view

<?xml version="1.0" encoding="utf-8"?>
   android:orientation="vertical" >

      android:layout_height="wrap_content" >

      android:layout_height="wrap_content" >

following will be the content of src/main/java/myListFragment.java file.before writing to code, need to follow few steps as shown below

  • Create a class MyListFragment and extend it to ListFragment.

  • Inside the onCreateView() method , inflate the view with above defined list_fragment xml layout.

  • Inside the onActivityCreated() method , create a arrayadapter from resource ie using String array R.array.planet which you can find inside the string.xml and set this adapter to listview and also set the onItem click Listener.

  • Inside the OnItemClickListener() method , display a toast message with Item name which is being clicked.

package com.example.tutorialspoint7.myapplication;

import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MyListFragment extends ListFragment implements OnItemClickListener {
   public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.list_fragment, container, false);
      return view;

   public void onActivityCreated(Bundle savedInstanceState) {
      ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), 
         R.array.Planets, android.R.layout.simple_list_item_1);

   public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
      Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();

Following code will be the content of MainActivity.java

package com.example.tutorialspoint7.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

   protected void onCreate(Bundle savedInstanceState) {

following code will be the content of manifest.xml, which has placed at res/AndroidManifest.xml

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

      <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

Running the Application

Let's try to run our SimpleListFragment application we just created. I assume you had created your AVD while doing environment set-up. To run the app from Android Studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Android installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

Android list fragment