disable an ImageButton?
I wanted to leave an ImageButton
is disabled (not clickable) but have used android: enabled = "false"
开发者_如何学JAVAand does't work.
Does anyone know how to disable an ImageButton
?
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.
You can use the android:clickable
attribute on the XML, or the setClickable(boolean)
method from your code.
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);
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.
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"
.
精彩评论