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

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Jeeva V
  • 153
  • 1
  • 1
  • 9
  • Please mark your code as code, and explain what you expect to happen as well as what actually happens. – Dragonthoughts Jul 03 '18 at 10:34
  • 2
    Possible duplicate of [Android changing Floating Action Button color](https://stackoverflow.com/questions/30969455/android-changing-floating-action-button-color) – Kamlesh Jul 03 '18 at 10:38
  • Using xml : app:backgroundTint="YOUR_COLOR_CODE" Using programatically : floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor("YOUR_COLOR_CODE"))); – Dhara Jani Jul 03 '18 at 10:42
  • Please make sure you are using android.support.design.widget.FloatingActionButton – Lokesh Desai Jul 03 '18 at 10:50
  • floatingButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor("YOUR_COLOR_CODE"))); //this code is not working for me. – Jeeva V Jul 03 '18 at 11:15

7 Answers7

7

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>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • It 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 ? – SebastienRieu Nov 25 '21 at 15:07
  • @SebastienRieu why should it change other properties? You are extending the current style using parent="@style/Widget.MaterialComponents.FloatingActionButton" – Gabriele Mariotti Nov 25 '21 at 15:11
  • 1
    You're right. It's because my button is an ExtendedFloatingActionButton so I had to apply the corresponding style. @style/Widget.MaterialComponents.ExtendedFloatingActionButton. Thanks ! – SebastienRieu Nov 25 '21 at 15:33
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*));
}
hamster121
  • 378
  • 1
  • 4
  • 13
3

Please try this.

app:backgroundTint="@android:color/darker_gray"
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
2

Simply use this attribute:

android:backgroundTint="@color/yourColor"
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
2

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

Mrudul Tora
  • 715
  • 8
  • 14
0

A simple solution to your problem would be to change your default colorAccent.

As stated in Android Developers-FloatingActionButton :

The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).

If it is not possible to change your color accent, regarding the requirements of your project, but you need to implement this programmatically you can post your code and I'll be more than happy to update my answer.

0

You could use

app:backgroundTint="@color/colorAccent"

like below

<android.support.design.widget.FloatingActionButton
        android:id="@+id/add_student_house"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/colorAccent"
        android:src="@drawable/ic_house_add"/>

just note it is app:backgroundTint not android:backgroundTint

Also you could do it by adding style:

<style name="AppTheme.FloatingActionButton" >
        <item name="colorAccent">@color/colorAccent</item>
</style>

And give this style as theme in floating action button like below:

android:theme="@style/AppTheme.FloatingActionButton"

<android.support.design.widget.FloatingActionButton
    android:id="@+id/add_student_house"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.FloatingActionButton"
    android:src="@drawable/ic_house_add"/>
devu mani
  • 337
  • 6
  • 12