Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable an ImageButton

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?

like image 457
Dalmas Avatar asked Nov 19 '11 18:11

Dalmas


People also ask

How to disable Image button in android?

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.

How to disable Image button in javascript?

Each button is an enable/disable button red(Disable) Green(Enable).

How do you scale ImageButton?

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.


1 Answers

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.

like image 127
Oleg Vaskevich Avatar answered Oct 08 '22 20:10

Oleg Vaskevich