Android Simple ListFragment Using ArrayAdapter

By | May 24, 2014

In the previous series we have already seen an example on fragments in android , In this tutorial we shall see how to create a simple list fragment that uses array adapter to bind data to items of listview .

Project Detail

Project Name SimpleListFragment
Package com.pavan.listfragmentdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

String Constant

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

file : strings.xml

<?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”>


XML Layout

After Initializing String constants , next step is creating xml layout so i will create two xml files inside res/layout.

  1. activity_main.xml .
  2. list_fragment.xml .

1. activity_main.xml

Inside activity_main.xml file we have LinearLayout tag as parent view and fragment tag as a child view , and for this fragment tag we will be inflating it with list_fragment.xml .


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

        android:layout_height=”match_parent” />


2. list_fragment.xml

  • Inside the list_fragment.xml file we have LinearLayout tag as parent view , and ListView and TextView act as childview to it .
  • Here listview and textview takes a special kind of id’s @android:id/list and @android:id/empty.
  • The purpose of  TextView is used to show some text message when listview is empty .


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

        android:layout_height=”wrap_content” >

        android:layout_height=”wrap_content” >


List Fragment 

  1. Create a class MyListFragment and extend it to ListFragment .
  2. Inside the onCreateView() method , inflate the view with above defined list_fragment xml layout .
  3. 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 .
  4. Inside the OnItemClickListener() method , display a toast message with Item name which is being clicked .

package com.pavan.listfragmentdemo;

import android.annotation.SuppressLint;
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<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);


void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        Toast.makeText(getActivity(), “Item: ” + position, Toast.LENGTH_SHORT)


Main Activity 

Finally Inside the inside the onCreate() , method set the content of the view with above defined activity_main.xml file


package com.pavan.listfragmentdemo;

import android.os.Bundle;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {


  • Sir, how to make intent for each item, such when I click Saturnur, it open
    Sorry my bad english

  • Check out this tutorials ( ) ,
    i hope this is what you are looking for

  • i m using android studion 1.5 and i try your coding but it's not work please correct me

  • how to create listview inside the listfragment itself.for example,i having having a list of shopping,education ,medical etc which has further some of their sub listview for example "shopping" has amazon,flipkart,etc then what will be the exact thing …I have to do

  • Thanks very much.
    It's quite easy to follow.