Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change floating action button color?

fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));

This is not working. How to change background color of floating action button

like image 280
Jeeva V Avatar asked Jul 03 '18 10:07

Jeeva V


5 Answers

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 color colorSecondary 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>
like image 123
Gabriele Mariotti Avatar answered Oct 19 '22 17:10

Gabriele Mariotti


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*));
}
like image 3
hamster121 Avatar answered Oct 19 '22 17:10

hamster121


Please try this.

app:backgroundTint="@android:color/darker_gray"
like image 3
Agilanbu Avatar answered Oct 19 '22 17:10

Agilanbu


Simply use this attribute:

android:backgroundTint="@color/yourColor"
like image 2
Sumit Shukla Avatar answered Oct 19 '22 17:10

Sumit Shukla


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().

like image 2
Mrudul Tora Avatar answered Oct 19 '22 18:10

Mrudul Tora