user954299
user954299

Reputation: 195

setting onclick image for image button in android

I have a image button for which I have loaded a image in my XML using android:src tag. I have few questions based on image button :

  1. my image is resized for aspect ratio 130:51 but when I set my image button width and height to wrap_content I see image is loaded as crop I want it to full fit into the image button so that the spacing should not be visible.

  2. when I run the app and tap on the image button I want a feel of state change. But currently I see it looks like a static image and when I tap it really doesn't show the push state changes. To achieve I want to load another image when user does tap event so he can see the state change and feel that button is clicked.

please help me on this.

Upvotes: 0

Views: 187

Answers (2)

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21117

1) my_image.setScaleType(ScaleType.FIT_XY);

2)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
            android:drawable="@drawable/btn_cab_done_pressed_example" />
        <item android:state_focused="true" android:state_enabled="true"
            android:drawable="@drawable/btn_cab_done_focused_example" />
        <item android:state_enabled="true"
            android:drawable="@drawable/btn_cab_done_default_example" />
    </selector>

Upvotes: 0

Armaan Stranger
Armaan Stranger

Reputation: 3140

Try this:

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

save this XML as any name you want.

and then just set background of button with this XML. so this will change the images as per given states.

Upvotes: 1

Related Questions