0

I followed this thread Change the color of a disabled button in android and successfully created two different styled buttons for enabled/disabled state.

However the difference is in the background color of the button: When disabled, the background color is green, when enabled it's blue.

How can I change the text color of the button from white to greyish depending on its state?

layout.xml

<Button
    android:id="@+id/loginButton"
    style="@style/StandardButton"
    android:enabled="false"
    android:layout_width="@dimen/standard_width"
    android:layout_height="@dimen/standard_height"
    android:layout_marginTop="8dp"
    android:onClick="onSignup"
    android:text="@string/signup_button_text"/>

styles.xml

<style name="StandardButton">
    <item name="android:background">@drawable/standard_button_rounded</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">20sp</item>
    <item name="android:layout_gravity">center_horizontal</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:foreground">?attr/selectableItemBackground</item>
    <item name="android:clickable">true</item>
</style>

standard_button_rounded.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/standard_button_rounded_enabled" android:state_enabled="true" />
    <item android:drawable="@drawable/standard_button_rounded_disabled" android:state_enabled="false" />
</selector>

standard_button_rounded_enabled.xml:

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

    <solid android:color="@color/colorAccent"/>
    <corners android:radius="@dimen/standard_button_radius"/>

</shape>

standard_button_rounded_disabled.xml:

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

    <solid android:color="@color/green"/>
    <corners android:radius="@dimen/standard_button_radius"/>

</shape>
sir-haver
  • 3,096
  • 7
  • 41
  • 85
  • You do something very similar with a `` for the `textColor`. The accepted answer on the linked duplicate shows how to do that. – Mike M. Dec 18 '19 at 03:35
  • Yeah, they tried a different (unworkable) approach, so the question isn't quite what they should've asked. The solution, though, is exactly what you're looking for. Cheers! – Mike M. Dec 18 '19 at 03:39

0 Answers0