This looks easy, but I'm not able to disable an ImageButton. It continues to receive click events, and its appearance don't change like a standard Button would.
There are some similar questions on SO, but they don't help me.
Even with a very simple layout like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical">      <ImageButton         android:id="@+id/btn_call"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:clickable="false"         android:enabled="false"         android:src="@android:drawable/sym_action_call" />  </LinearLayout>   The button is still enabled and I can click it.
What's strange is that if I change the ImageButton to a simple Button, then it works as expected. The button becomes disabled and unclickable. I don't understand. Does anyone have an idea?
If you want make enable = false in XML for ImageButton you have to add android:focusable="false" and android:clickable="false" . In fact, setting android:enabled to "false" DOES disable an ImageButton; the button doesn't receive lick events anymore.
Each button is an enable/disable button red(Disable) Green(Enable).
How to programatically resize and show them? Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling. All of these attributes can be set in code on each ImageButton at runtime.
Here's the code I use to disable an ImageButton and make it look grayed out:
/**  * Sets the specified image buttonto the given state, while modifying or  * "graying-out" the icon as well  *   * @param enabled The state of the menu item  * @param item The menu item to modify  * @param iconResId The icon ID  */ public static void setImageButtonEnabled(Context ctxt, boolean enabled, ImageButton item,         int iconResId) {     item.setEnabled(enabled);     Drawable originalIcon = ctxt.getResources().getDrawable(iconResId);     Drawable icon = enabled ? originalIcon : convertDrawableToGrayScale(originalIcon);     item.setImageDrawable(icon); }  /**  * Mutates and applies a filter that converts the given drawable to a Gray  * image. This method may be used to simulate the color of disable icons in  * Honeycomb's ActionBar.  *   * @return a mutated version of the given drawable with a color filter  *         applied.  */ public static Drawable convertDrawableToGrayScale(Drawable drawable) {     if (drawable == null) {         return null;     }     Drawable res = drawable.mutate();     res.setColorFilter(Color.GRAY, Mode.SRC_IN);     return res; }   Simply call setImageButtonEnabled(); the only downside is you need the image's resource ID in here because it's not possible to revert a transformed icon back into the original.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With