S.A.Jay
S.A.Jay

Reputation: 421

Android: ImageButton not showing states pressed

I have an ImageButton and I swear I have everything set up right but it is not showing the different icon when a state is pressed. Here is what I have.

ImageButton

        <!-- HOME BUTTON -->
    <ImageButton
        android:id="@+id/home"
        android:layout_width="45dp"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:contentDescription="Home" 
        android:src="@drawable/title_home"
        android:layout_alignParentLeft="true"
        android:onClick="onClickHome" />

Title Home

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/title_home_pressed"/>
<item android:state_focused="true" android:drawable="@drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/title_home"/>
</selector>

And yes these are all valid images, and they are different. title_home is the only one that is showing up.

I would appreciate any ideas?

Upvotes: 0

Views: 680

Answers (2)

Shubhayu
Shubhayu

Reputation: 13562

android:src="@drawable/title_home"

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

Same name. Change your xml file name to title_home_btn_selector. Then make the first one as

android:src="@drawable/title_home_btn_selector"

Upvotes: 0

user1276567
user1276567

Reputation:

Notice how you are referring to @drawable/title_home in the first block of code, but you are intending to refer to the xml file. Although in the second block of code you are referring to another @drawable/title_home which I am assuming is an image.

Because of this I can only assume that from your information your first block of code is reading the image and not the xml file. To fix this, I believe changing the names of one of them would help.

Upvotes: 1

Related Questions