Skip to content Skip to sidebar Skip to footer

Why Does The Material Fab Not Change Colors When Disabled?

I am disabling the Material Floating Action button but the color does not change when disabled is set to true. I thought Material has a theme for FAB's and when disabled it should

Solution 1:

I suspect that this is the culprit:

app:backgroundTint="@color/color_primary"

This is going to tint the color of your FAB regardless of its state.

You could solve this by setting the tint to a ColorStateList instead of a raw color value. That is, create a file named fab_color.xml in your res/color/ directory, and include this:

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_enabled="false"android:color="your gray here"/><itemandroid:color="@color/color_primary"/></selector>

And change your tint to this instead:

app:backgroundTint="@color/fab_color"

Alternatively, you could adjust your Activity's theme such that the default color of the FAB is the color you want (@color/color_primary) and then remove the app:backgroundTint attr altogether.

Solution 2:

The version 1.2.0introduced the support for enabled/disabled states in the FloatingActionButton.

Now the default style support the disabled state and the background color is based on the colorOnSurface when disabled:

<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:color="?attr/colorSecondary"android:state_enabled="true"/><itemandroid:alpha="0.12"android:color="?attr/colorOnSurface"/></selector>

enter image description hereenter image description here

You can change it using the app:backgroundTint attribute with a custom selector or using:

<com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:theme="@style/ThemeOverlay.Custom.FloatingActionButton"../>

with:

<stylename="ThemeOverlay.Custom.FloatingActionButton"parent=""><itemname="colorOnSurface">@color/....</item></style>

Post a Comment for "Why Does The Material Fab Not Change Colors When Disabled?"