Change the color of a disabled button in android

nixgadget picture nixgadget · Oct 12, 2015 · Viewed 31.6k times · Source

Is there a way to change the color of a disabled button in android through styles or some other form ?

I currently have the following,

drawable/button_default.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_default_shape"/>
</selector>

drawable/button_default_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/button_default_background"/>
    <corners android:radius="3dp"/>
</shape>

values/styles.xml

<style name="AppTheme.Button">
    <item name="android:background">@drawable/button_default</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">10dp</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">17sp</item>
    <item name="android:textAppearance">@style/CustomFontAppearance</item>
</style>

Answer

AndroidEnthusiast picture AndroidEnthusiast · Oct 12, 2015

You'll have to use a selector for different drawables in those states.

You can create a selector like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_enabled_drawable" android:state_enabled="true" />
    <item android:drawable="@drawable/your_disabled_drawable" android:state_enabled="false" />
    <!-- default state-->
    <item android:drawable="@drawable/your_enabled_drawable" />
</selector>