644

I have been trying to change Material's Floating Action Button color, but without success.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/profile_edit_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_mode_edit_white_24dp" />

I have tried to add:

android:background="@color/mycolor"

or via code:

FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.profile_edit_fab);
fab.setBackgroundColor(Color.parseColor("#mycolor"));

or

fab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#mycolor")));

But none of the above worked. I have also tried the solutions in the proposed duplicate question, but none of them works; the button remained green and also became a square.

P.S. It would be also nice to know how to add ripple effect, couldn't understand that either.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jjang
  • 11,250
  • 11
  • 51
  • 87
  • 2
    possible duplicate of [Change color of Floating Action Button from Appcompat 22.2.0 programmatically](http://stackoverflow.com/questions/30966222/change-color-of-floating-action-button-from-appcompat-22-2-0-programmatically) – Jared Burrows Jun 21 '15 at 21:57
  • Ripple effect is not available on pre-lollipop devices because it utilizes a new [RenderThread](http://developer.android.com/about/versions/lollipop.html). – tachyonflux Jun 21 '15 at 22:00
  • @karaokyo ok but how am I doing it? – Jjang Jun 22 '15 at 17:20
  • 52
    Google does a real bad job in my opinion to make these things accesible – Joop Jul 17 '15 at 13:51
  • To do this programmatically and backwards compatible, see http://stackoverflow.com/questions/30966222/change-color-of-floating-action-button-from-appcompat-22-2-0-programmatically/38618011#38618011 – Ralph Pina Jul 27 '16 at 16:01

27 Answers27

1227

As described in the documentation, by default it takes the color set in styles.xml attribute colorAccent.

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 you wish to change the color

  • in XML with attribute app:backgroundTint
<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" >
  • in code with .setBackgroundTintList (answer below by ywwynm)

As @Dantalian mentioned in the comments, if you wish to change the icon color for Design Support Library up to v22 (inclusive), you can use

android:tint="@color/white"     

For Design Support Library since v23 for you can use:

app:tint="@color/white"   

Also with androidX libraries you need to set a 0dp border in your xml layout:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:backgroundTint="@color/orange"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="normal" />
Marko
  • 20,385
  • 13
  • 48
  • 64
262

Vijet Badigannavar's answer is correct but using ColorStateList is usually complicated and he didn't tell us how to do it. Since we often focus on changing View's color in normal and pressed state, I'm going to add more details:

  1. If you want to change FAB's color in normal state, you can just write

    mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
    
  2. If you want to change FAB's color in pressed state, thanks for Design Support Library 22.2.1, you can just write

    mFab.setRippleColor(your color in int);
    

    By setting this attribute, when you long-pressed the FAB, a ripple with your color will appear at your touch point and reveal into whole surface of FAB. Please notice that it won't change FAB's color in normal state. Below API 21(Lollipop), there is no ripple effect but FAB's color will still change when you're pressing it.

Finally, if you want to implement more complex effect for states, then you should dig deeply into ColorStateList, here is a SO question discussing it: How do I create ColorStateList programmatically?.

UPDATE: Thanks for @Kaitlyn's comment. To remove stroke of FAB using backgroundTint as its color, you can set app:borderWidth="0dp" in your xml.

Community
  • 1
  • 1
ywwynm
  • 11,573
  • 7
  • 37
  • 53
  • 3
    Thanks for this, I didn't know how to use ColorStateList. However, your code leaves my FAB with a stroke (outline) of the theme accent color. Do you have any idea how I can remove that stroke (or change it's color, rather) and have a FAB in a special color that looks like that special color is the accent color? – Kaitlyn Hanrahan Aug 20 '15 at 15:08
  • @KaitlynHanrahan Please see my update. Thanks for your comment. – ywwynm Aug 21 '15 at 09:12
  • That works perfect -- Lollypop and Kitkat look the same, even both have that little shading so the FAB pops against the same color in the background (I have it partially over the Toolbar). Thanks so much! It's simple but I don't know if I ever would have found that on my own. – Kaitlyn Hanrahan Aug 22 '15 at 17:21
  • Is there a way how to set `app:borderWidth` programatically? Thanks – Štarke Oct 22 '15 at 06:50
  • @Štarke Well, after looking at the source code of `FloatingActionButton`, I'm afraid that you cannot set it programatically right now since there is no setter for `mBorderWidth`. – ywwynm Oct 22 '15 at 13:07
  • @mutkan Yes, as far as I know, someone committed a bug about this and Google fixed it in 23.2.1. So please update your project. – ywwynm Apr 06 '16 at 16:20
  • One thing to be careful of is that #123456 and #12345678 has a difference. The first one is transparent (probably assumes 00 for the first two hex integers). – goelakash Apr 26 '16 at 14:12
  • `getResources().getColor(int id)` is deprecated now needs `getResources().getColor(int id, Theme theme)` – KingKongCoder Oct 06 '17 at 16:59
  • That's what I needed. Works with android.support.v7 – ieselisra Apr 17 '18 at 21:39
142

As Vasil Valchev noted in a comment it is simpler than it looks, but there is a subtle difference that I wasn't noticing in my XML.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/profile_edit_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_mode_edit_white_24dp"
    app:backgroundTint="@android:color/white"/>

Notice it is:

app:backgroundTint="@android:color/white"

and not

android:backgroundTint="@android:color/white"
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • 8
    This is important because these are two completely different things: `android:backgroundTint=""` is from *Api level 21* and part of the Lollipop Material Design and will be ignored below Lollipop. Also it doesn't change the `FAB` colour completely because it is not a solid color. You have to use the `app:` one to make it work. – creativecreatorormaybenot Feb 05 '17 at 15:33
  • 1
    Thanks, using `android:color` caused my app to crash, and it was driving me nuts! Thanks for saving my app and what's left of my sanity ;-) – Kapenaar Mar 30 '17 at 17:47
  • @Kapenaar no problem, was scratching my head for a while as well before noticing ;) – HenriqueMS Mar 31 '17 at 12:56
  • How would this work programatically since on the java side there is no difference like app: and android: in the XML. – marienke May 22 '17 at 07:58
  • @marienke you can set it with a ColorStatesList, post a new question and post the link here so I can answer it ;) – HenriqueMS May 22 '17 at 08:31
62

if you try to change color of FAB by using app, there some problem. frame of button have different color, so what you must to do:

app:backgroundTint="@android:color/transparent"

and in code set the color:

actionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.white)));
ergo
  • 1,116
  • 9
  • 6
  • 2
    This work for normal colors. If I have given alpha color like #44000000 the frame color issue is reproducible. – Dinesh Feb 12 '16 at 09:38
  • 7
    getResources.getColor() is deprecated, consider using ContextCompat.getColor(context, your colour); instead. Hope it helps – Tinashe Chinyanga May 06 '16 at 09:24
55

just use,

app:backgroundTint="@color/colorPrimary"

dont use,

android:backgroundTint="@color/colorPrimary"
Ashana.Jackol
  • 3,064
  • 28
  • 22
  • hmmmm this cause android studio autocomplete give result use android:backgroundTint , this answer verry good – Yudi karma Apr 25 '19 at 07:31
35

The FAB is colored based on your colorAccent.

<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
    <item name="colorAccent">@color/accent</item>
</style>
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • Well, the default color is not green. Where did you set a green color? – tachyonflux Jun 22 '15 at 17:44
  • 1
    I did not. As you see, this xml is the only place I set up the layout of the button... – Jjang Jun 22 '15 at 17:50
  • Maybe you should create a new project with just the FAB and post the code for the entire thing. – tachyonflux Jun 22 '15 at 17:59
  • I really don't see anything I can add. Maybe this: My AppTheme's parent is "@style/Theme.AppCompat.Light", min version is 8 and target version is 21. Maybe it's related to this theme that no color is affecting the fab? I can't use Material theme because it's only for min api 21. – Jjang Jun 22 '15 at 18:11
  • Well, it works great for me. So, if you aren't willing to do anything else to help replicate the issue, then I guess that's it. – tachyonflux Jun 22 '15 at 18:17
  • Could you please post an answer with all related code, so I open up a new project / copy it to mine and see if that works at least? – Jjang Jun 22 '15 at 18:20
  • 3
    Wow... I basically just asked you to do the same thing and you weren't willing to do it. It is quite hypocritical of you to believe that your code was adequate, but that my addition to your code is not adequate. It falls to the asker to create an [MCVE](http://stackoverflow.com/help/mcve). Thanks. – tachyonflux Jun 22 '15 at 18:27
  • how will it be in the XML though ? – MBH Nov 07 '15 at 18:54
  • You must modify the theme you're using for that Activity in your AndroidManifest.xml. – Jason Cheladyn Feb 25 '16 at 19:22
  • 5
    Not anymore, based on [Theme Attribute Mapping](https://material.io/develop/android/components/floating-action-button/) the Floating Action Button now (I verified myself) is colored to **colorSecondary** (_com.google.android.material:material:1.1.0-alpha02_) – ievgen Feb 04 '19 at 07:26
  • The correct way. Tinting gives fill but tiny stroke of `colorAccent` remains around FAB. To use this needed to set theme for FAB: `` then add `android:theme="@style/FabTheme"`. – MainActivity Nov 20 '19 at 16:31
28
mFab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext,R.color.mColor)));
atoMerz
  • 7,534
  • 16
  • 61
  • 101
Reza Isfandyari
  • 281
  • 3
  • 2
21

New theme attribute mapping for Floating Action Button in material 1.1.0

In your app theme:

  • Set colorSecondary to set a color for background of FAB (maps to backgroundTint)
  • Set colorOnSecondary to set a color for icon/text and ripple color of FAB (maps to tint and rippleColor)

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- ...whatever else you declare in your app theme.. -->
    <!-- Set colorSecondary to change background of FAB (backgroundTint) -->
    <item name="colorSecondary">@color/colorSecondary</item>
    <!-- Customize colorSecondary to change icon/text of FAB (maps to tint and rippleColor) -->
    <item name="colorOnSecondary">@android:color/white</item>
</style>
luca992
  • 1,548
  • 22
  • 27
17

Other solutions may work. This is the 10 pound gorilla approach that has the advantage of being broadly applicable in this and similar cases:

Styles.xml:

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

your layout xml:

<android.support.design.widget.FloatingActionButton
       android:theme="AppTheme.FloatingAccentButtonOverlay"
       ...
 </android.support.design.widget.FloatingActionButton>
Robin Davies
  • 7,547
  • 1
  • 35
  • 50
  • Hmm, seems like this should work, but I'm getting compiler errors in the layout file `String types not allowed (at 'theme' with value 'AppTheme.FloatingAccentButtonOverlay')`. Maybe I'm missing a point with the styles.xml.... – SMBiggs Aug 25 '16 at 06:30
  • @ScottBiggs use `android:theme="@style/AppTheme.FloatingAccentButtonOverlay"` but this solutin does not work for me either way... – jpact Jan 14 '17 at 21:20
  • What about API 16 which does not have coloarAccent atrribute? – Dushyant Suthar Apr 23 '17 at 09:51
16

With the Material Theme and the material components FloatingActionButton by default it takes the color set in styles.xml attribute colorSecondary.

  • You can use the app:backgroundTint attribute in xml:
<com.google.android.material.floatingactionbutton.FloatingActionButton
       ...
       app:backgroundTint=".."
       app:srcCompat="@drawable/ic_plus_24"/>
  • You can use fab.setBackgroundTintList();

  • You can customize your 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">#00f</item>
    <!-- color used by the icon -->
    <item name="tint">@color/...</item>
  </style>
  • starting from version 1.1.0 of material components you can use the new materialThemeOverlay attribute to override the default colors only for some components:
  <style name="MyFloatingActionButton" parent="@style/Widget.MaterialComponents.FloatingActionButton">
    <item name="materialThemeOverlay">@style/MyFabOverlay</item>
  </style>

  <style name="MyFabOverlay">
    <item name="colorSecondary">@color/custom2</item>
    <!-- color used by the icon -->
    <item name="colorOnSecondary">@color/...</item>
  </style>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
14

The document suggests that it takes the @color/accent by default. But we can override it on code by using

fab.setBackgroundTintList(ColorStateList)

Also remember,

The minimum API version to use this library is 15 so you need to update it! if you dont want to do it then you need to define a custom drawable and decorate it!

  • works, but then I have to target api 21+. if I target api 8 can't call this method. – Jjang Jun 26 '15 at 15:12
  • Just include this design library that has been released by google "com.android.support:design:22.2.0". This can be used on devices that are non-lollipop versions! cheers!! – Vijet Badigannavar Jun 27 '15 at 18:10
  • of course I do, but this is the error on this code: Call requires API level 21 (current min is 8): android.widget.ImageView#setBackgroundTintList – Jjang Jun 27 '15 at 18:23
  • The minimum API version to use this library is 15 so you need to update it! if you dont want to do it then you need to define a custom drawable and decorate it! – Vijet Badigannavar Jun 27 '15 at 18:53
  • Oh thanks! I didn't know, thought it was 21 since this is what the error said. Please post this as an answer and I'll accept it! – Jjang Jun 28 '15 at 17:29
14

Changing Floating action button background color by using below line

app:backgroundTint="@color/blue"

Changing Floating action button icon color

android:tint="@color/white"     
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
9

Thanks to autocomplete. I got lucky after a few hit and trials:

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:backgroundTint="@color/whicheverColorYouLike"

-- or -- (both are basically the same thing)

    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:backgroundTint="@color/whicheverColorYouLike"

This worked for me on API Version 17 with design library 23.1.0.

9

I got the same problem and its all snatching my hair. Thanks for this https://stackoverflow.com/a/35697105/5228412

What we can do..

 favourite_fab.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.favourite_selected));

it works fine for me and wish for others who'll reach here.

Community
  • 1
  • 1
Purvik Rana
  • 193
  • 2
  • 11
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Bhargav Rao Mar 22 '16 at 17:51
9
 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:elevation="6dp"
    app:backgroundTint="@color/colorAccent"
    app:pressedTranslationZ="12dp"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/add"/>

Note that you add colors in res/values/color.xml and include the attribute in your fab

   app:backgroundTint="@color/addedColor"
Gavine Joyce
  • 389
  • 3
  • 8
7

for Material design, I just changed the floating action button color like this, Add the below two lines in your Floating action button xml. And done,

 android:backgroundTint="@color/colorPrimaryDark"
 app:borderWidth="0dp"
Jwala Kumar
  • 525
  • 7
  • 9
6

When using Data Binding you can do something like this:

android:backgroundTint="@{item.selected ? @color/selected : @color/unselected}"

I have made a very simple example

Jan Málek
  • 531
  • 8
  • 21
6

If you have a Floating Action Button with no drawable you can change the tint programmatically using:

fab.getBackground().mutate().setTint(ContextCompat.getColor(yourContext, R.color.anyColor));
Tom
  • 61
  • 1
  • 1
4

i did it like this android:background="@color/colorAccent" i just go to folder res then click on folder values and then on colors.xml in colors.xml I just change the color of colorAccent and call it in android:background and its done

3

The point we are missing is that before you set the color on the button, it's important to work on the value you want for this color. So you can go to values > color. You will find the default ones, but you can also create colors by copping and pasting them, changing the colors and names. Then... when you go to change the color of the floating button (in activity_main), you can choose the one you have created

Exemple - code on values > colors with default colors + 3 more colors I've created:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="corBotaoFoto">#f52411</color>
    <color name="corPar">#8e8f93</color>
    <color name="corImpar">#494848</color>

</resources>

Now my Floating Action Button with the color I've created and named "corPar":

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
        android:tint="#ffffff"
        app:backgroundTint="@color/corPar"/>

It worked for me. Good Luck!

Nidia F.
  • 31
  • 1
3

My solution, which worked for me with Data Binding

val color = ContextCompat.getColor(context, R.color.colorPrimary)
binding.fab.backgroundTintList = ColorStateList.valueOf(getColor)
PeterPazmandi
  • 533
  • 10
  • 13
2

in Kotlin:

val gray = getColor(requireContext(), R.color.green)
binding.fabSubmit.backgroundTintList = ColorStateList.valueOf(gray)
fullmoon
  • 8,030
  • 5
  • 43
  • 58
1

You can use Extended, so set app:iconTint like this:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        app:icon="@drawable/d0"
        app:iconTint="@color/white"
         />
Mori
  • 2,653
  • 18
  • 24
0

You can use this code in case you want to change the color programmatically

floating.setBackgroundTintList(getResources().getColorStateList(R.color.vermelho));
OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
0

add colors in color.xml file

add colors in color.xml file and then add this line of code... floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.fab2_color)));

Progga Ilma
  • 578
  • 6
  • 8
0

use

app:backgroundTint="@color/orange" in


<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_share_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/share"
        app:backgroundTint="@color/orange"
        app:fabSize="mini"
        app:layout_anchorGravity="end|bottom|center" />



</androidx.coordinatorlayout.widget.CoordinatorLayout>

0

You can achieve this in Kotlin by the below code.

 binding.fabAddPostMenu.backgroundTintList =
    ColorStateList.valueOf(ContextCompat.getColor(this,R.color.complaint_red))
thevikasnayak
  • 559
  • 5
  • 13