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 turn light grey. I do not want to add code to change the background every time it is enabled/disabled.
I am currently on material version: 1.1.0
In the code I just set the fab to disabled by fab.isEnabled = false
Here is the xml
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/save_reservation_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/keyline_2"
app:backgroundTint="@color/color_primary"
android:src="@drawable/ic_save_black_72dp"
app:tint="@color/color_on_primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
This is what the disabled/enabled fab looks like:
It should look something like this:
I suspect that this is the culprit:
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 namedfab_color.xml
in yourres/color/
directory, and include this:And change your tint to this instead:
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 theapp:backgroundTint
attr altogether.