0

We have a relatively simply Android item template on a ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@android:drawable/list_selector_background">
  <!--Encounter Type and Start-->
  <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@color/mid_grey"
      android:paddingTop="12dp"
      android:paddingRight="24dp"
      android:paddingBottom="12dp"
      android:paddingLeft="24dp">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textStyle="bold"
        android:textSize="@dimen/text_size_large"
        android:text="Main Item Heading"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10:45"
        android:layout_toLeftOf="@+id/startTimeRelative"
        android:textColor="@color/grey1"
        android:textSize="@dimen/text_size_large" />
    <TextView
        android:id="@+id/startTimeRelative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12sp"
        android:layout_alignParentRight="true"
        android:text="Today"
        android:textColor="@color/grey1"
        android:textSize="@dimen/text_size_large" />
  </RelativeLayout>
  <!--Patient Name and Avatar-->
  <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:paddingTop="8dp"
      android:paddingRight="24dp"
      android:paddingBottom="8dp"
      android:paddingLeft="24dp">
    <ImageView
        android:id="@+id/patientAvatar"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:scaleType="fitXY"
        android:src="@drawable/patient_avatar" />
    <LinearLayout
        android:layout_toRightOf="@+id/patientAvatar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
      <TextView
          android:id="@+id/patientname"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Subsection Heading"
          android:textColor="#000000"
          android:textSize="@dimen/text_size_large"
          android:textStyle="bold"
          android:ellipsize="end"
          android:maxLines="1" />
    </LinearLayout>
  </RelativeLayout>
  <!--</LinearLayout>-->
</LinearLayout>

That displays this layout

enter image description here

I want to set the background of the items when they are pressed or selected. I am trying to do this by setting the item's background to a custom item selector

android:background="@android:drawable/list_selector_background"

The is is the selector 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/debug_blue" />
    <item android:state_checked="true" android:drawable="@color/debug_green" />
    <item android:state_pressed="true" android:drawable="@color/debug_blue" />
</selector>

The problem is that because I am setting the background colour's of Views within the item the Selector does not show through as expected. I have removed one of the inner colours (used in the "Subsection Heading" in the screenshot) and set that as the colour of the Listview. That means the blue shows through when the item is pressed on the lower part of the template, as seen in this screenshot

enter image description here

All the Android examples showing item selectors working with simple item layouts that have one or no background colour set.

How can I set the whole item's background whilst still having one or more background colours on child view's, when not selected?

I have looked at setting drawSelectorOnTop on the ListView but that did not help

Pat Long - Munkii Yebee
  • 3,592
  • 2
  • 34
  • 68
  • If you don't want whole item painted on the click event then why did you put selector in the parent properties instead of putting custom selector for every part of the item? – tompadre May 03 '17 at 13:23
  • I DO want the whole item painted when pressed\selected. Can you point me at an example where there are selectors for each part of the item – Pat Long - Munkii Yebee May 03 '17 at 13:42
  • by layouts/selector http://stackoverflow.com/a/30729969/5577679 and by listeners http://stackoverflow.com/a/22417695/5577679 – tompadre May 03 '17 at 13:47
  • I didn't know I could apply different selectors to child views. That has worked. I also needed to set clickable=false on the RelativeLayouts for the whole item to behave as expected when pressed – Pat Long - Munkii Yebee May 04 '17 at 16:02

0 Answers0