MikeC
MikeC

Reputation: 223

Android imagebutton does not flash on click

I've implemented an ImageButton. All works well except when I press on it, it doesn't "flash" before moving on (to another activity). Does Android has intrinsic "flash" for ImageButton or I have to write/animate that explicitly inside onClickEvent? or use Selector?

Thanks in advance for all your help.

Upvotes: 3

Views: 3705

Answers (5)

arianoo
arianoo

Reputation: 667

If you let your ImageButton keep its background and don't set it to null, it will act like a normal button and will flash when clicking, exactly like other buttons.The way to hide the background while it is still there:

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingTop="1dp"
    android:src="@drawable/squareicon" />

the paddings won't let the background be visible and make the button act like other buttons.

Upvotes: 2

Kumar Bibek
Kumar Bibek

Reputation: 9117

For ImageButtons, you shouldn't be setting it's background. Set the image as "src", and then the ImageButton would still have the default flashing property.

Here, I guess, you are setting a background to your ImageButton, which is not required I suppose.

Upvotes: 0

MikeC
MikeC

Reputation: 223

Ended up doing it programatically as I also had problem with using "selector" with Eclipse complaining "unable to resolve the file".

public void flashBtn (final ImageButton myBtnToFlash){
    myBtnToFlash.setBackgroundResource(R.drawable.glossy_button_green_rectangle);
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
         public void run() { 
             myBtnToFlash.setBackgroundResource(0);
         } 
    }, 50);

}

Upvotes: 1

dbrettschneider
dbrettschneider

Reputation: 3183

If you have an image for the normal button and one image for the pressed state you should use a selector. I think it's the easiest way.

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

Upvotes: 1

Related Questions