Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

ImageButton background is not changed when it is selected

I am having a ImageButton and when I press it, Its background/src should be changed i have created a selector for it but it doesn't seem to be working.

can anyone tell me why it is not working.

navigation_button_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <layer-list>
            <item>
                <bitmap android:antialias="true" android:src="@drawable/t_navigation_dw" />
            </item>
            <item>
                <bitmap android:gravity="left" android:src="@drawable/left_padding" android:tileMode="disabled" />
            </item>
        </layer-list>
    </item>
    <item  android:state_pressed="false" android:state_focused="true">
        <layer-list>
            <item>
                <bitmap android:src="@drawable/t_navigation_dw" />
            </item>
            <item>
                <bitmap android:gravity="left" android:src="@drawable/left_padding" android:tileMode="disabled" />
            </item>
        </layer-list>
    </item>
    <item android:drawable="@drawable/t_navigation_up">
    </item>
</selector>

ImageButton initialization in layout xml.

 <ImageButton
        android:id="@+id/navigation_image_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:src="@drawable/navigation_button_drawable" />

Upvotes: 0

Views: 335

Answers (1)

SilentKiller
SilentKiller

Reputation: 6942

You are not setting seletor xml file to the ImageButton set

Replace

android:background="@android:color/transparent"

with

android:background="@drawable/navigation_image_button_selector"

To keep selected

inside the onClick of the ImageView call view.setSelected(true);
in order to force the content of the ImageView to change.

Upvotes: 2

Related Questions