Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android imagebutton does not flash on click

Tags:

android

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.

like image 451
MikeC Avatar asked Apr 18 '26 21:04

MikeC


2 Answers

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.

like image 113
arianoo Avatar answered Apr 22 '26 17:04

arianoo


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>
like image 40
dbrettschneider Avatar answered Apr 22 '26 17:04

dbrettschneider