How do I set the disabled color of a button with AppCompat?
instead of using color for your button, you should use a background with selectors. Here is the demo code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape android:shape="rectangle">
<solid android:color="@color/yourEnabledColor" />
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="@color/yourDisabledColor" />
</shape>
</item>
</selector>
You aren't using the Widget.AppCompat.Button.Colored
style correctly. You're using a parent style (Widget.AppCompat.Button.Colored
), but applying it as a theme. This effectively means that the Widget.AppCompat.Button.Colored
part is being ignored entirely and you are instead just changing the default color of the button (which works, but doesn't handle the disabled case).
Instead, you should use a ThemeOverlay
and apply the Colored
style separately:
<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark">
<!-- customize colorButtonNormal for the disable color -->
<!-- customize colorAccent for the enabled color -->
</style>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_login_login_button"
android:theme="@style/AccentButton"
style="@style/Widget.AppCompat.Button.Colored"/>
As mentioned in this answer on using the Widget.AppCompat.Button.Colored
style, the disabled color is controlled by colorButtonNormal
and the enabled color is controlled by colorAccent
. By using the ThemeOverlay.AppCompat.Dark
, the textColor
is automatically changed to dark, meaning you may not need the custom ThemeOverlay
at all.