How to change floating action button color?
12,030
Solution 1
Using the material components FloatingActionButton
and a material theme by default it uses the color with the attribute colorSecondary
.
To change this color you have different options:
- You can use the
app:backgroundTint
attribute in xml:
<com.google.android.material.floatingactionbutton.FloatingActionButton
...
app:backgroundTint=".." />
- You can use a custom style using the
<item name="backgroundTint">
attribute
<!--<item name="floatingActionButtonStyle">@style/Widget.MaterialComponents.FloatingActionButton</item> -->
<style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">....</item>
</style>
- starting from version 1.1.0 of material components you can also use the new
materialThemeOverlay
attribute to override the default colorcolorSecondary
only for a component:
<style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
<item name="materialThemeOverlay">@style/MyFabOverlay</item>
</style>
<style name="MyFabOverlay">
<item name="colorSecondary">@color/custom2</item>
</style>
Solution 2
Please try this.
app:backgroundTint="@android:color/darker_gray"
Solution 3
You can use this code to change the background color:
FloatingActionButton button = findViewById(R.id.your_button);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
((AppCompatButton) button).setSupportBackgroundTintList(ColorStateList.valueOf(*your color in integer*));
} else {
ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(*your color in integer*));
}
Solution 4
Simply use this attribute:
android:backgroundTint="@color/yourColor"
Solution 5
Your code is completely correct. I also wrote in a similar way for changing background color of floating action button programatically. Just check your xml code once. My XML Code :
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
app:borderWidth="0dp"
android:id="@+id/fab"
android:layout_margin="16dp"
app:elevation="8dp"/>
Remember here, keeping borderWidth="0dp"
is important.
Now, in Java Code :
fab = view.findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(required_color));
Here you can replace required_color using Color.parseColor()
or ContextCompat.getColor()
.
Related videos on Youtube
Author by
Jeeva V
Updated on November 01, 2020Comments
-
Jeeva V over 3 years
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));
This is not working. How to change background color of floating action button
-
Kamlesh almost 6 yearsPossible duplicate of Android changing Floating Action Button color
-
-
Someone Somewhere about 5 yearsthe OP is using
setBackgroundTintList
and it's not working... so this answer doesn't help -
SebastienRieu over 2 yearsIt works good but it changes other button properties as text style for example (all caps and letter spacing for sure, certainly others). Have you got a solution ?
-
Gabriele Mariotti over 2 years@SebastienRieu why should it change other properties? You are extending the current style using parent="@style/Widget.MaterialComponents.FloatingActionButton"
-
SebastienRieu over 2 yearsYou're right. It's because my button is an ExtendedFloatingActionButton so I had to apply the corresponding style. @style/Widget.MaterialComponents.ExtendedFloatingActionButton. Thanks !