2

I am selecting multiple item in listview for delete . I can delete multiple item . The code is as follows :

smsListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public void onItemCheckedStateChanged(ActionMode mode,
                        int position, long id, boolean checked) {
                    // Capture total checked items
                    final int checkedCount = smsListView.getCheckedItemCount();
                    // Set the CAB title according to total checked items
                    mode.setTitle(checkedCount + " Selected");
                    View v = smsListView.getChildAt(position
                            - smsListView.getFirstVisiblePosition());
                    // Calls toggleSelection method from ListViewAdapter Class
                    ((SmsArrayAdapter) arrayAdapter).toggleSelection(position,v);

                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()) {
                    case R.id.delete:
                        // Calls getSelectedIds method from ListViewAdapter Class
                        SparseBooleanArray selected = ((SmsArrayAdapter) arrayAdapter)
                                .getSelectedIds();
                        // Captures all selected ids with a loop
                        for (int i = (selected.size() - 1); i >= 0; i--) {
                            if (selected.valueAt(i)) {
                                SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
                                // Remove selected items following the ids
                                arrayAdapter.remove(selecteditem);
                            }
                        }
                        // Close CAB
                        mode.finish();
                        return true;
                    default:
                        return false;
                    }
                }

                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.getMenuInflater().inflate(R.menu.activity_main, menu);
                    return true;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    // TODO Auto-generated method stub
                    ((SmsArrayAdapter) arrayAdapter).removeSelection();
                }

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


            });

But I want to change color of the rows that I have selected . Currently there are no color on selection item in the list .

enter image description here

I have tried the following .

Step no 1: write below line to to your listview item layout

android:background="?android:attr/activatedBackgroundIndicator"

Step no 2: write below line to style.xml file

 <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="android:activatedBackgroundIndicator">@drawable/muliple_selected_item</item>
 </style>

Step no 3: Create muliple_selected_item.xml into Drawable folder and write below code.

<?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/lvMultipleSelectionColor" />
    <item android:state_checked="true" android:drawable="@color/lvMultipleSelectionColor" />
    <item android:state_pressed="true" android:drawable="@color/lvMultipleSelectionColor" />
    <item android:drawable="@android:color/transparent" />
</selector>

But by this code , all itemsof the list are colored when I select any item of the listview . I want to change background color to those items only which I have selected . How can I do this ?

The listview layoput is as follows :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/smsItemContainerRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_gravity="center_horizontal|top"
            android:text="SMS Inbox"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/unread_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="-20dp" 
            android:background="@drawable/notification_bg"
            android:gravity="center"
            android:text="88"
            android:textColor="@android:color/background_light"
            android:textSize="20sp"
            android:textStyle="bold"   
            android:layout_alignParentRight="true"
            android:visibility="invisible" />



    </RelativeLayout>

    <ListView
        android:id="@+id/SMSList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"  />


</LinearLayout>

The rows layout is as follows :

<?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" 
    android:background="@drawable/muliple_selected_item" >>

    <TextView
        android:id="@+id/textView_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SMS From"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
         <TextView
        android:id="@+id/textView_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="SMS : "  />

         <TextView
        android:id="@+id/textView_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:text="Time : "  />

    </LinearLayout>

</LinearLayout>

The muliple_selected_item.xml is as follows : .

<?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/lvMultipleSelectionColor" />
    <item android:state_checked="true" android:drawable="@color/lvMultipleSelectionColor" />
    <item android:state_pressed="true" android:drawable="@color/lvMultipleSelectionColor" />
    <item android:drawable="@android:color/transparent" />
</selector>
osimer pothe
  • 2,827
  • 14
  • 54
  • 92

4 Answers4

2

You need to set Multiple Select Choice mode to your ListView

smsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

You need to create Drawable file for your ListView's row Layout and use Drawable as background in that layout.

Here is Drawable file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/menuitem_press_background_color" android:state_activated="true" />
    <item android:drawable="@color/white" />
</selector>

EDIT:

remove changeBackgroundColor() used in selectView() and check if it works

You can refer this Selecting multiple items in ListView. it might help you

Community
  • 1
  • 1
Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
2

Just put the line below in your single row xml file(the xml file in which you define the define the layout for single row of listview):-

android:background="?android:attr/activatedBackgroundIndicator"

And no need to edit any more xml files. For more details, please visit the link below :-

Click here to go to link ...!

Hope this helps :)

Tom
  • 184
  • 2
  • 7
  • It works, it changes the color, but in my case it changes the color to black and I can't see the view, how to define a specific color in it without affecting initial items color? – CGR Oct 20 '17 at 22:28
0

You can use drawables for that like below,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true"
android:drawable="@android:color/darker_gray" />
</selector>

and set it as background of your row layout,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_background_activated" >
</RelativeLayout>
Hardik Chauhan
  • 2,750
  • 15
  • 30
0

You can Achieve it in this way:-

  1. First add property(variable) isSelected to SMSItem class with default Value false.
  2. on onItemCheckedStateChanged method Execution set value for variable isSelected.
  3. on getView method of Adapter you have to chech value for isSelected variable. then make a decison base on it, if false then set default background and if true the set another background to perticular row.
  4. call notifyDataSetChanged() method on Adepter from onItemCheckedStateChanged.

on Your Request, Im posting Code:-

add this to onItemCheckedStateChanged method

SMSItem selecteditem = (SMSItem) arrayAdapter.getItem(selected.keyAt(i));
selecteditem.setSelection(checked);
notifydatasetChanged();

and manage getView yourself

Hradesh Kumar
  • 1,765
  • 15
  • 20