Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight selected item in ListView on Android

I have been making an application that works with ListViews in Android, and I can't make it so that the selected (chacked) item has a different background. I'm using CHOICE_MODE_SINGLE. This is how my code looks so far:


The ListView that I use:
(inside layout.xml)

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/selector_test" >
</ListView>

The TextView layout I use in the adapter:
(listItem.xml)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="23sp"
    android:textStyle="bold" />

This is where I add the adapter:

mListAdapter = new ArrayAdapter<String>(this, R.layout.listItem, mList);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mAuthorAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        String selected = mList.get(position);
        // ...
        mListView.setItemChecked(position, true);
    }
});

I'm sure that the proper item is checked on click, because when I call getCheckedItemPosition(), it returns the proper value.


And now, the two things I tried in order to highlight the checked item:


Selector drawable:
(selector_test.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_longAnimTime">

    <item android:state_checked="true"><shape>
            <solid android:color="@color/red" />
        </shape></item>
    <item android:state_selected="true"><shape>
            <solid android:color="@color/blue" />
        </shape></item>
    <item android:state_pressed="true"><shape>
            <solid android:color="@color/green" />
        </shape></item>

</selector>

I add it to .xml with:

android:listSelector="@drawable/selector_test"

Background drawable:
(background_test.xml)

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

    <item android:drawable="@color/red" android:state_checked="true"/>
    <item android:drawable="@color/blue" android:state_selected="true"/>
    <item android:drawable="@color/green"/>

</selector>

I add it to .xml with:

android:background="@drawable/background_test"

I've tried adding the selector and the background to both listView.xml and listItem.xml, but the only thing that changes is the default background color, and the color of the selector when the item is pressed (or held).
android:state_checked="true" and android:state_selected="true" seem to do nothing.

I can change the background by overriding the getView() method in ArrayAdapter and invoking setBackgroundColor() in it if the view is selected, and it does change the background, but also gets rid of the selector entirely. Also, I don't really like to override classes just to change one line of code, especially if that same thing can be achieved in a different way.

So what I'm asking is, is there a way to highlight checked item in ListView by adding a selector or background drawable to it, and I am just doing it wrong, or will I have to make it work some other way.

Thanks in advance! :)

like image 620
Velja Avatar asked Mar 03 '14 18:03

Velja


3 Answers

add this line in onStart of your activity

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

where lv is instance of listView

then override this method and add the following lines to it.

 @Override
public void onListItemClick(ListView l, View v, int position, long id) {


    // Set the item as checked to be highlighted 
    lv.setItemChecked(position, true);
            v.setBackgroundColor(Color.BLUE);

        conversationAdapter.notifyDataSetChanged();

}

and then change the color of previous selected item's background back to normal in getView method of your custom adapter

like image 166
Ravinder Bhandari Avatar answered Oct 17 '22 11:10

Ravinder Bhandari


Try this:

listViewDay.setItemChecked(position, true);
listViewDay.setSelection(position);

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@drawable/list_day_selector" >

    <TextView
        android:id="@+id/txtItemDay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:text="22"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

</LinearLayout>

list_selector.xml

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

    <item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
    <item android:drawable="@drawable/bg_with_left_arrow" android:state_pressed="true"/>
    <item android:drawable="@drawable/bg_with_left_arrow" android:state_activated="true"/>

</selector>
like image 5
Luc Avatar answered Oct 17 '22 13:10

Luc


Programmatically, use setSelector. For example:

lv.setSelector(R.color.abc_background_cache_hint_selector_material_dark);
like image 3
Oleksandr Bondarchuk Avatar answered Oct 17 '22 13:10

Oleksandr Bondarchuk