Todd
Todd

Reputation: 3009

Is there a style for disabled buttons in Android?

Is there a style/item within themes.xml for setting a button's disabled appearance? I've set the button's appearance via the android:colorButtonNormal style/item, but that results in the same style/item being applied to both enabled and disabled buttons. I'd have thought there'd be a style/item like android:colorButtonDisabled, but there isn't (at least not by that name).

I'm coding in Java 8 and Android 13 (API 33).

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ShooterHelper" parent="Theme.AppCompat.DayNight">
        <!-- Customize your light theme here. -->

        <item name="android:statusBarColor">@color/material_dynamic_neutral30</item>
        <item name="colorPrimary">@color/material_dynamic_neutral50</item>
        <item name="android:colorBackground">@color/material_dynamic_neutral90</item>
        <item name="android:colorButtonNormal">@color/material_dynamic_neutral_variant80</item>

EDIT: Based on Djes's answer I now have the following.

Res folders:

enter image description here

res/color/button_background_state.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--    <item android:color="@color/material_dynamic_neutral80" android:state_enabled="true"/>-->
<!--    <item android:color="@color/material_dynamic_neutral50" android:state_enabled="false"/>-->
    <item android:color="@color/green" android:state_enabled="true"/>
    <item android:color="@color/blue" android:state_enabled="false"/>
</selector>

res/values/themes/themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.ShooterHelper" parent="Theme.AppCompat.Light">
        <item name="android:statusBarColor">@color/material_dynamic_neutral30</item>
        <item name="colorPrimary">@color/material_dynamic_neutral50</item>
        <item name="android:colorBackground">@color/material_dynamic_neutral90</item>
        <item name="android:backgroundTint">@color/material_dynamic_neutral100</item>
        <item name="android:textColor">@color/black</item>
    </style>

    <style name="ButtonStyle">
        <item name="backgroundColor">@color/button_background_state</item>
    </style>

<!--    <style name="Theme.ShooterHelper" parent="Base.Theme.ShooterHelper" />-->
</resources>

And my res/layout/fivebyfive.xml file

<Button
    android:id="@+id/btnScore"
    android:layout_width="150dp"
    android:layout_height="48dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="30dp"
    android:enabled="false"
    android:text="Score"
    style="@style/ButtonStyle"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/lblMikes" />

<Button
    android:id="@+id/btnReset"
    android:layout_width="150dp"
    android:layout_height="48dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="30dp"
    android:enabled="true"
    android:text="Reset"
    style="@style/ButtonStyle"
    app:layout_constraintStart_toEndOf="@+id/btnScore"
    app:layout_constraintTop_toBottomOf="@+id/txtMikes" />

But, now I get NO background color on either button, whether enabled or not. This image shows the score button disabled and the reset button enabled

enter image description here

And this image shows both buttons enabled. Note: the style says they should be green.

enter image description here

Upvotes: 0

Views: 72

Answers (1)

djes
djes

Reputation: 36

You can make your own style and use selector:

in res/color create button_background_state.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_is_enable" android:state_enabled="true"/>
    <item android:color="@color/color_is_disable" android:state_enabled="false"/>
</selector>

in themes.xml

<style name="ButtonStyle">
        <item name="backgroundColor">@color/button_background_state</item>
</style>

Use the style in the button layout:

<Button
    ...
    style="@style/ButtonStyle"
    ...
    />

You can define other colors in the same way.

Upvotes: 1

Related Questions