KiKo
KiKo

Reputation: 1710

Selector don't work on ImageButton - Android

I want to change background color the ImageButton when I click on it. So I'm creating selector...

button_click.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_normal" />
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true" />

</selector>

For this selector I'm creating two drawables: button_normal.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/white_transparent" />
            <corners android:radius="2dp" />
            <stroke android:color="@color/shadow" android:width="1dp" />
        </shape>
    </item>
</selector>

and button_pressed.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFCC00" />
            <corners android:radius="2dp" />
            <stroke android:color="@color/shadow" android:width="1dp" />
        </shape>
    </item>
</selector>

Everything is the same, only I'm changing color. This is my colors.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="gray">#EEEEEE</color>

    <color name="primaryColor">#2196F3</color>
    <color name="primaryColorDark">#1976D2</color>
    <color name="accentColor">#009688</color>

    <color name="shadow">#1A000000</color>
    <color name="text_color">#B6B7B9</color>
    <color name="button_clicked">#80000000</color>
    <color name="white_transparent">#BFFFFFFF</color>

</resources>

And I'm using this selector in my main_screen.xml file on ImageButton:

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_click"
        android:padding="@dimen/activity_vertical_margin"
        android:clickable="true"
        android:id="@+id/btnSinoLoc"
        android:src="@drawable/find_sino"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:contentDescription="@null"
        android:maxHeight="40dp"
        android:maxWidth="40dp"
        android:adjustViewBounds="true"
        android:layout_alignTop="@+id/map"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

But this is not working... I got my ImageButton as I want, but when I press, the color is not changing. So what I'm missing, or there is other way for doing this?

Upvotes: 0

Views: 928

Answers (1)

Opiatefuchs
Opiatefuchs

Reputation: 9870

You have to define also a item for statePressed false:

   <item android:drawable="@drawable/button_normal" android:state_pressed="false" />

and set the src of Your ImageButton:

   android:src="@drawable/button_click"

You described the selector xml has a name of button_click.xml, so You have to call it in the src attribute. Also, the drawables has to be real drawables and not two selectors. So You can make a shape...

   <?xml version="1.0" encoding="UTF-8"?> 
   <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle"> 
        <solid android:color="#FFCC00" />
        <corners android:radius="2dp" />
        <stroke android:color="@color/shadow" android:width="1dp" />
    </shape>

Upvotes: 3

Related Questions