Disabled color state of Material button
- Create the folder
/res/color
(in yourres
directory). - Add a new color resource file here, named something like color_states_materialbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:color="@color/colorDisabled" />
<item android:color="@color/colorEnabled" />
</selector>
- Create a style in styles.xml with one of the
Widget.MaterialComponents.Button
styles as its parent and your color state list as thebackgrountTint
tag:
<style name="MaterialButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="backgroundTint">@color/color_states_materialbutton</item>
</style>
- Set your style on the
MaterialButton
in your layout:
<com.google.android.material.button.MaterialButton
style="@style/MaterialButtonStyle"
android:id="@+id/disabled_material_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/button_label_disabled"/>
With the default style Widget.MaterialComponents.Button
the default selector used as backgroundTint
handles the disabled state without any changes:
It is the default selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>
Just use:
<com.google.android.material.button.MaterialButton
android:enabled="false"
..>
If you want to change the disabled color you can use a custom selector
<com.google.android.material.button.MaterialButton
app:backgroundTint="@color/my_selector"
..>
or you can override the colors used in the default selector:
<com.google.android.material.button.MaterialButton
android:theme="@style/button_overlay"
..>
with:
<style name="button_overlay">
<item name="colorOnSurface">@color/my_color</item>
</style>
- Create a new Android Resource Directory inside your res folder. Give it the name "color" (see attached image).
- Create a Color Resource File with the name "button_disabled" (see attached image).
- Place the below code into the button_disabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#FD7E14" android:alpha="0.45" />
<item android:color="#FD7E14" />
</selector>
- Locate your values/styles.xml and add the below code
<style name="AppMaterialButton" parent="Widget.MaterialComponents.Button.UnelevatedButton"> <item name="android:backgroundTint">@color/button_disabled</item> </style>
- Goto your layout/activity_filename.xml file and add this
android:enabled="false"
to your button widget.
<com.google.android.material.button.MaterialButton
android:enabled="false"
android:id="@+id/button_Join"
style="@style/AppMaterialButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="next"
app:cornerRadius="0dp" />
Android Resource Directory