35

I wanted to leave an ImageButton is disabled (not clickable) but have used android: enabled = "false" and does't work.

Does anyone know how to disable an ImageButton?

NoWar
  • 36,338
  • 80
  • 323
  • 498
Pedro
  • 451
  • 2
  • 6
  • 5

5 Answers5

29

If you want to show the button as disabled (if you have that set up in an xml drawable file) doing both setClickable(false) AND setEnabled(false) will do the trick.

GKeps
  • 456
  • 5
  • 6
  • 3
    Note that setting these attributes does prevent the imagebutton from being clicked, but it doesn't grey the button out like it would on a normal button – Cody Jun 10 '17 at 22:45
17

You can use the android:clickable attribute on the XML, or the setClickable(boolean) method from your code.

Cristian
  • 198,401
  • 62
  • 356
  • 264
10

When setting a clicklistener for the ImageButton, under the hood android resets the attribute clickable to true. That's why setting android:clickable="false" in xml is not helpful.

In addition, setting the attribute android:enabled="false" in the xml didn't work for me as well.

What did work is only setting via the code:

ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);
NoWar
  • 36,338
  • 80
  • 323
  • 498
user2924714
  • 1,918
  • 1
  • 19
  • 26
9

If you want to disable and "grey out" the image, I use the following (Kotlin):

Disable:

chevron_left.imageAlpha = 75 // 0 being transparent and 255 being opaque
chevron_left.isEnabled = false

Enable:

chevron_left.imageAlpha = 255
chevron_left.isEnabled = true

XML:

<ImageButton
            android:id="@+id/chevron_left"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginBottom="4dp"
            android:layout_marginStart="4dp"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:src="@drawable/chevron_left"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

Note that the your background color will define the color of the disabled state. Depends on your desired result.

Javatar
  • 2,518
  • 1
  • 31
  • 43
-1

ImageButton like ImageView does not have android:enabled="false" attribute, because it is attribute of TextView. If you want make enable = false in XML for ImageButton you have to add android:focusable="false" and android:clickable="false".

Yuri Misyac
  • 4,364
  • 2
  • 16
  • 32
  • In fact, setting android:enabled to "false" DOES disable an ImageButton; the button doesn't receive lick events anymore. What it does not do is change the appearance of the button. – FractalBob Jul 21 '20 at 04:07