3

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.

MikeC
  • 223
  • 1
  • 4
  • 15
  • What does you mean under "flash"? Button may have three states: normal, focused and pressed. Does you mean pressed state? – Anton Derevyanko Apr 28 '11 at 09:54
  • @Anton - Right now the ImageButton does not show if it has been pressed-then-released or anything. I just want a visual feedback to the user like flashing the background (image button has a transparent background to start with) in green color twice, before moving on. – MikeC Apr 28 '11 at 10:07

5 Answers5

2

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.

arianoo
  • 657
  • 14
  • 22
1

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>
dbrettschneider
  • 3,173
  • 1
  • 29
  • 28
  • I do not have different images for the ImageButton in its various states. All I want is a visual cue to show the user that he has pressed the button (a flash or twice of the IB's background) then moves on. – MikeC Apr 28 '11 at 10:17
  • I think you can simply set a color in the selector for that: – dbrettschneider Apr 28 '11 at 12:13
  • Thx. I'll have to try that later. Packing for a trip now... I guess Selector is the way to go. – MikeC Apr 28 '11 at 12:19
1

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);

}
MikeC
  • 223
  • 1
  • 4
  • 15
0

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.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

Android - Textview change color on changing of state

Community
  • 1
  • 1