I have a button defined as shown below. When I want to disable it I use my_btn.setEnabled(false)
, but I would also like to grey it out. How can I do that?
I tried the above solutions but none of them seemed to work for me. I went with the following option:
<!-- button_color_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_enabled="true"/>
<item android:color="@color/colorAccentLight" android:state_enabled="false"/>
</selector>
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/button_color_selector"
.../>
Set Clickable as false and change the backgroung color as:
callButton.setClickable(false);
callButton.setBackgroundColor(Color.parseColor("#808080"));
You have to provide 3 or 4 states in your btn_defaut.xml
as a selector.
You will provide effect and background for the states accordingly.
Here is a detailed discussion: Standard Android Button with a different color