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″?>
<resources>

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

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 .

file:activity_main.xml

<?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=”com.pavan.listfragmentdemo.MyListFragment”
        android:layout_width=”match_parent”
        android:layout_height=”match_parent” />

</LinearLayout>

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 .

file:list_fragment.xml

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

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 .

file:MyListFragment.java

package com.pavan.listfragmentdemo;

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 {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    
        super.onActivityCreated(savedInstanceState);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1);
      
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);

    }

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

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

    }
}

Main Activity 

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

file: MainActivity.java

package com.pavan.listfragmentdemo;

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);
    }

}

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

  • Check out this tutorials ( http://www.tutorialsbuzz.com/2013/11/android-activity-for-each-items-in.html ) ,
    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.