Can't get background_activated drawable to work


#1

I am nearly certain the code is identical to that in the book, and it’s really not that complex a task - create the background_activated.xml in res/drawable/, add the android:background property to the relative layout of the list_item_crime.xml.

For some reason though the selector never shows. Even without the selector added the list items stay the same when selected (in the book (Fig. 18.4) it shows that the text goes slightly gray to indicate selection at this stage, but my items stay unchanged). They are deleted appropriately so I know they are actually being selected. Could it be something to do with the theme? The AppTheme is set to “Theme.AppCompat.Light.DarkActionBar”, the device I’m testing on is a Galaxy S4 with Android 4.3. I’ve also tried adding android:drawSelectorOnTop=“true” to the relative layout list item but it has no effect either :frowning:

background_activated.xml

[code]<?xml version="1.0" encoding="utf-8"?>

[/code]

list_item_crime.xml

[code]<?xml version="1.0" encoding="utf-8"?>

<CheckBox
    android:id="@+id/crime_list_item_solvedCheckBox"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:gravity="center"
    android:layout_alignParentRight="true"
    android:enabled="false"
    android:focusable="false"
    android:padding="4dp"/>

<TextView
    android:id="@+id/crime_list_item_titleTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
    android:textStyle="bold"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:text="Crime Title"/>

<TextView
    android:id="@+id/crime_list_item_dateTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/crime_list_item_titleTextView"
    android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"
    android:paddingTop="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:text="Crime Date"/>

[/code]


#2

I’ve got the same problem. But it looks like it’s not seeing the background_activated.xml file I created in the res/drawable folder because on the android:background line in list_item_crime.xml, I get a warning that says “Unexpected text found in layout file: “android:background=”@drawable/background_activated”>"


#3

WingnutWilson - I think your code for list_item_crime.xml should say “android:background” instead of “android:drawable”

Like this:

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

trevorh - Have you tried to clean your Project (Project --> Clean)? Also maybe running Lint would give a helpful hint? I’m just trying to think about how to get the XML file to show up… There’s always the option of restarting eclipse too, if you haven’t tried that.


#4

[quote=“redwinged”]
trevorh - Have you tried to clean your Project (Project --> Clean)? Also maybe running Lint would give a helpful hint? I’m just trying to think about how to get the XML file to show up… There’s always the option of restarting eclipse too, if you haven’t tried that.[/quote]

Thanks for the suggestions. Lint actually told me that background_activate wasn’t be in used. :slight_smile: It finally turned out I had placed the new line below the existing lines without moving the closing “>”. :blush: