Android ListView Items Active InActive state selector

By | April 11, 2014
We have already seen how to create ListView in android in previous subsequent series , by default items in the list view does not have selector to know whether the item is active or inactive

 Here the term

  • active : when the user holds or in touch with ListView Item.
  • inactive : when the user releases his touch from ListView Item.

Lets See An Example

Project Detail

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

String Constants

file : res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

    <string name=”app_name”>ListViewStateDemo</string>
    <string name=”action_settings”>Settings</string>
    <string name=”hello_world”>Hello world!</string>

    <string-array name=”Planets”>
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
        <item>Pluto</item>
    </string-array>

</resources>

Drawable XML Resource

State file preview
active item_bg_active.xml
inactive item_bg_inactive.xml

file: drawable/item_bg_active.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shape=”rectangle” >

    <solid android:color=”#78DBFF” />

</shape>

file: drawable/item_bg_inactive.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shape=”rectangle” >

    <solid android:color=”#FFF952″ />

</shape>

file: drawable/item_selector.xml

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

    <item 
           android:drawable=”@drawable/item_bg_inactive”
           android:state_activated=”false”> </item>

    <item
         android:drawable=”@drawable/item_bg_active”
         android:state_activated=”true”></item>

</selector>

XML Layout

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

    <ListView
        android:id=”@+id/listView1″
        android:layout_width=”match_parent”
        android:layout_height=”match_parent”
        android:listSelector=”@drawable/item_selector” >
    </ListView>

</LinearLayout>

Main Activity

file : MainActivity.java

package com.pavan.listviewstatedemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 String[] planets;
 ListView mlistview;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  planets = getResources().getStringArray(R.array.Planets);

  mlistview = (ListView) findViewById(R.id.listView1);

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, planets);

  mlistview.setAdapter(adapter);
  mlistview.setActivated(true);

 }

}

  • good toturial………………………………….