Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a android ListView item selector to use state_checked
    primarykey
    data
    text
    <p>Tearing my hair out trying to get an Android ListView to do what I want. </p> <p>I want to have a ListView in single choice mode with a custom row layout that has a different background color for selected, pressed and checked (i.e. the choice is shown by a color rather than a check mark - this is what I would normally call the "selection" but selection in android seems line I'm about to choose before I press it)</p> <p>I thought of trying a background selector with the three states in it. It works fine for state_selected and state_pressed, but not state_checked. So I created a CheckableRelativeLayout that extends RelativeLayout and implements Checkable and used for the view of each row.</p> <p>A simplified version is shown here:</p> <pre><code>&lt;my.package.CheckableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bkg_selector"&gt; &gt; &lt;ImageView android:id="@+id/animage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /&gt; &lt;/my.package.CheckableRelativeLayout&gt; </code></pre> <p>bkg_selector looks like</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/purple" /&gt; &lt;item android:state_checked="true" android:drawable="@drawable/red" /&gt; &lt;item android:state_selected="true" android:drawable="@drawable/darkpurple" /&gt; &lt;item android:drawable="@drawable/black" /&gt; &lt;/selector&gt; </code></pre> <p>The colors are defined elsewhere.</p> <p>This still didn't work. So in the custom ListAdapter I tracked the "checked" row and tried (in getView)</p> <p>if( position == checkedPosition ) ret.getBackground().setState(CHECKED_STATE_SET);</p> <p>And it STILL doesn't work. How can I get it to do what I want?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload