disable an ImageButton?

感情迁移 提交于 2019-11-28 10:42:04

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.

user2924714

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);

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".

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!