Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background of checked items in a listview in multiple select mode in android

Tags:

android

I've a ListActivity

public class MyActivity extends ListActivity{

    protected void onCreate(Bundle savedstate){
        super.onCreate(savedstate);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {


        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            Log.i("xxx", "onPrepareActionMode");
            return false;
        }


        public void onDestroyActionMode(ActionMode mode) {
            // TODO Auto-generated method stub

        }


        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return true;
        }


        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }


        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            Log.i("xxx", "pos: "+position+", checked: "+checked);
            mode.setTitle(getListView().getCheckedItemCount()+" checked");

        }
    });
    }

}

Here I can see the items getting checked but background of checked items is not changing. I know a way how to do it, have your CustomAdapter and in getView() set the background of the views that are checked. But I believe there is much simpler way to handle it. Could you tell me if there is anyway to achieve it?

Attaching the Screenshot

like image 477
sujith Avatar asked Dec 15 '22 17:12

sujith


1 Answers

Ok, i got the answer by going through the source code of other app which does that. Set a statelist drawable as background for list item. In selector state_activated = true give the background drawable.

like image 171
sujith Avatar answered May 23 '23 23:05

sujith