faizal
faizal

Reputation: 3565

selected items in list view not getting highlighted

I am trying to implement the MultiChoiceModeListener to use the contextual action bar in my fragment(v4 support library). But i am having problems with showing that an item in the list view has been selected. What i expect is that the selected items gets highlighted. What happens in reality is that just the action bar shows up indicating the number of items that have been selected but there is no indication of which item was selected.

    if (Utils.hasHoneycomb()) {
        Log.d("faizal","has honeycomb");

        //Enable selection of multiple chat messages
        lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

        //Handle Action mode events
        lv_chatMessages
                .setMultiChoiceModeListener(new MultiChoiceModeListener() {

                    @Override
                    public boolean onActionItemClicked(ActionMode arg0,
                            MenuItem arg1) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public boolean onCreateActionMode(ActionMode mode,
                            Menu menu) {
                        Log.d("faizal","oncreateactionmode");
                        MenuInflater inflater = mode.getMenuInflater();
                        inflater.inflate(R.menu.chatsession_contextmenu, menu);
                        return true;
                    }

                    @Override
                    public void onDestroyActionMode(ActionMode arg0) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onPrepareActionMode(ActionMode arg0,
                            Menu arg1) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onItemCheckedStateChanged(ActionMode mode,
                            int position, long id, boolean checked) {
                        Log.d("faizal","onItemCheckedStateChanged : " + position);
                        mode.setTitle(lv_chatMessages.getCheckedItemCount() + " selected");

                    }

                });
    }

I tried using listview.setSelection(position) in onItemCheckedStateChanged() but it did not make any difference.

I have also tried manually changing the background color of the selected item by using a row selector in my item layout file as below, but this does not make any difference either :

chat_list_item.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="wrap_content">

    <LinearLayout
        android:id ="@+id/wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rowselector">

        <TextView
            android:id="@+id/txt_chat"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:focusable="false"
            />

        </LinearLayout>
</LinearLayout>

rowselector.xml :

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

    <item android:state_selected="true" android:drawable="@color/yellow" />

</selector>

So is there a way to indicate which items have been selected?

Upvotes: 1

Views: 721

Answers (2)

faizal
faizal

Reputation: 3565

Use a state_activated instead of state_selected in rowselector.xml :

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

    <item android:state_activated="true" android:drawable="@color/yellow" />

</selector>

Thanks to @Luksprog

Upvotes: 2

CodeWarrior
CodeWarrior

Reputation: 5176

Change your selector to this

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

    <item android:drawable="@drawable/button_bg_normal" android:state_selected="true"></item>
    <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_bg_normal"></item>

</selector>

Upvotes: 0

Related Questions