Disable ImageButton

It looks simple, but I cannot disable ImageButton . It continues to receive click events, and its appearance does not change like a standard button.

There are some similar questions on SO, but they do not help me.

Even with a very simple layout:

 <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 on and I can click it.

Which is strange, if I changed ImageButton to a simple Button , then it works as expected. The button becomes disabled and unattractive. I do not understand. Anyone have an idea?

+23
android imagebutton
Nov 19 '11 at 18:24
source share
7 answers

ImageButton has a different inheritance chain, that is, it does not extend Button :

ImageButton < ImageView < View

He continues to receive click events.

Here's what happens when you install the click listener for a View :

 public void setOnClickListener(OnClickListener l) { if (!isClickable()) { setClickable(true); } mOnClickListener = l; } 

Therefore, if you set the listener, android:clickable="false" will change to android:clickable="true" .

and its appearance does not change like a standard button

You must provide a list of display states for the view so that it can set the appropriate image based on android:enabled . Do you have this? Or do you have a single image for your button?

EDIT: You can find information about StateListDrawable here . android:state_enabled is what you need to use in the list to tell the OS which image to use for this state.

EDIT 2: Since you really need to add a listener, you can check inside the listener if (!isEnabled()) { return; } else {/* process the event */} if (!isEnabled()) { return; } else {/* process the event */} if (!isEnabled()) { return; } else {/* process the event */} if (!isEnabled()) { return; } else {/* process the event */} .

+16
Nov 19 '11 at 18:49
source share
β€” -

Here is the code I use to disable ImageButton and make it gray:

 /** * 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 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; } 

Just call setImageButtonEnabled() ; the only drawback is that you need the image resource identifier here, because it is impossible to return the converted icon back to the original.

+42
Jan 02 '13 at
source share

Make sure that the view hierarchy does not have the same identifier, and you are not adding any lookups to it.

+2
Nov 19 '11 at 18:46
source share

if you want to disable the image button in the click event, set the "setEnabled" property to false

Example: imgButton.setEnabled(false);

+2
01 Oct '12 at 11:55
source share

Using the answer of Oleg Vaskevich . You can make an answer for Kotlin.

Create an extension function for ImageButton as follows:

 /** * 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 iconResId The icon ID */ fun ImageButton.setButtonEnabled(enabled: Boolean, iconResId: Int) { isEnabled = enabled val originalIcon = context.resources.getDrawable(iconResId) val icon = if (enabled) originalIcon else convertDrawableToGrayScale(originalIcon) setImageDrawable(icon) } 

And you become a little less dependent on the provision of context

0
Mar 31 '18 at 20:57
source share

I managed to create a solution inspired by Oleg Vaskevich's answer , but without having to pass the resource ID for drawing to setEnabled ().

Here is the Kotlin code inside the service module:

 fun Drawable.deepCopy(): Drawable = constantState?.newDrawable()?.mutate() ?: throw RuntimeException("Called on null Drawable!") fun Drawable.toGrayscale(): Drawable = deepCopy().apply { setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN) } fun ImageButton.setAndShowEnabled(enabled: Boolean) { if (enabled == isEnabled) return isEnabled = enabled if (enabled) { setImageDrawable(tag as Drawable) } else { if (tag == null) tag = drawable setImageDrawable(drawable.toGrayscale()) } } 

This can be used like this:

 val button: ImageButton = findViewById(...) // ... button.setAndShowEnabled(false) // launch async operation GlobalScope.launch { // do work here // unblock button button.setAndShowEnabled(true) } 
0
Nov 15 '18 at 12:07
source share

As the other answers have already said, you cannot disable ImageButton in the XML layout, as you can Button , but you can disable both the same at runtime:

In Java:

 button.setEnabled(false); // setEnabled(boolean) on TextView imgButton.setEnabled(false); // setEnabled(boolean) on View 

In both cases, the button is disabled - no click events fall into its onClickListener .

You can also change the icon color of a disabled ImageButton same way you change the color of text on a disabled Button , assuming that the icon can be changed.

In the XML layout:

 <Button ... android:textColor="@drawable/button_color_selector" /> <ImageButton ... android:tint="@drawable/button_color_selector" /> 

Now setEnable(boolean) for a Button or ImageButton changes the color of the text or icon according to the states in your button_color_selector.xml

0
Jan 22 '19 at 4:18
source share



All Articles