Android listview array adapter selected

android, java, layout, listview

Solution

This is becasue in touch mode there is no focused or selection. You're on the right track using the checked state. You just need to use a state drawable with different states.

Steps:

1) In your row_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/item_selector"
> 

2) Create a new xml file (match the name to the one you used in your row layout) in your drawable folder like so (note that I have a color.xml file set up so I can use @color/color, if you don't you can simply put a hex color code in there):

<?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/blue" />
</selector>

That's it. It's not reaql intuitive, but "activated" basically means "checked" when you have enabled the checked state.

With that setup (and enabling the list rows to be checkable) you will now have the background changed from whatever it is normally to blue when in the checked state. Add code to set that state in onTouch or onClick and you'll be all set. The background will stay that way until you touch another item.

Problem

i'm trying to add a contextual action mode to a listview, but i'm having some problems with the selection, if i make a`List1.setSelection(position)` it doesn't select anything, and if i make `List1.setItemChecked(position, true)` it works but it only changes the font color a little and i want it to change the background or something more notable, is there any way to detect the selection and manually and change the background, or i'm missing something? the list: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="singleChoice" android:drawSelectorOnTop="false"> </ListView> </RelativeLayout> ``` the adapter: ``` public class ServicesRowAdapter extends ArrayAdapter<String[]> { private final Activity context; private final ArrayList<String[]> names; static class ViewHolder { public TextView Id; public TextView Date; public RelativeLayout statusbar,bglayout; } public ServicesRowAdapter(Activity context, ArrayList<String[]> names) { super(context, R.layout.servicesrowlayout, names); this.context = context; this.names = names; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null) { LayoutInflater inflater = context.getLayoutInflater(); rowView = inflater.inflate(R.layout.servicesrowlayout, null); ViewHolder viewHolder = new ViewHolder(); viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel); viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel); rowView.setTag(viewHolder); } ViewHolder holder = (ViewHolder) rowView.getTag(); holder.Date.setText(names.get(position)[2]); holder.Id.setText(names.get(position)[1]); return rowView; } } ``` with the use of a layout: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/idlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="right" android:text="@+id/idlabel" android:textSize="20dp" android:width="70dp" > </TextView> <TextView android:id="@+id/datelabel" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/datelabel" android:textSize="20dp" android:layout_marginLeft="90dp" > </TextView> </RelativeLayout ```

Original source