Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML selector background not working on 2.3

I have a View that I set a selector background to that should reacht to touches. It does, but only on 4.x. On 2.3 it just doesn't react to touches. What could be the problem? Here's the layout:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/idee_baden"
    android:scaleType="centerInside" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/background_selector" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="8dp"
        android:ellipsize="end"
        android:padding="4dp"
        android:singleLine="true"
        android:textAppearance="@style/SmallTextBold"
        android:textColor="#ffffff" />
</RelativeLayout>

And this is the background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue9"
          android:state_pressed="true" />
    <item android:drawable="@drawable/black9" />
</selector>
like image 977
fweigl Avatar asked Nov 11 '22 14:11

fweigl


1 Answers

This functionality has been enabled since API 1 - so I don't think this has anything to do with the OS level being supported, but more to do with your selector code being poorly formatted.

I think your selector is not properly formatted. I think your 'item' tag sections should be filled out more completely, and you should have more of them.

Here is one of the selectors I use (more complete, but still missing some of the options regarding focus vs. pressed). Keep in mind that order matters (this is evaluated from top to bottom):

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/circle_button_on"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/circle_button_on"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/circle_button_off"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/circle_button_off"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>
like image 73
Booger Avatar answered Nov 14 '22 22:11

Booger