XX_brother
XX_brother

Reputation: 1697

some problems about replacing imagebutton‘s background

I use “selector” to replace imagebutton's background.Now the background can be replace only when I press the imagebutton.If I don't press it, the background will become original picture.I want to have this result: The original background is No.1 picture.After I press it,it will be replaced by No.2 picture even if I don't press it. enter image description here

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/nav_product"></item>
<item android:state_pressed="true" android:drawable="@drawable/nav_product_a"></item>

In the selector,it has many parameters.I don't know I should use which one.Or must I code in Activity?

Upvotes: 0

Views: 78

Answers (2)

Sunil Parmar
Sunil Parmar

Reputation: 1233

You can try following .xml file for your problem

<?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="false"
        android:drawable="@drawable/shape_btnopen_hover">
    </item>
    <item 
        android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_btnopen_hover">
    </item>
    <item 
        android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@drawable/shape_btnopen_hover">
    </item>
    <item android:drawable="@drawable/shape_btnopen">
    </item>
</selector>

Upvotes: 1

Vikram Bodicherla
Vikram Bodicherla

Reputation: 7123

Try

<item android:drawable="@drawable/nav_product"></item>

And play around with the other states: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Upvotes: 0

Related Questions