Android changing Floating Action Button color
Solution 1
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" />
Solution 2
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:
-
If you want to change
FAB
's color in normal state, you can just writemFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
-
If you want to change
FAB
's color in pressed state, thanks for Design Support Library 22.2.1, you can just writemFab.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 ofFAB
. Please notice that it won't changeFAB
's color in normal state. Below API 21(Lollipop), there is no ripple effect butFAB
'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.
Solution 3
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"
Solution 4
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)));
Solution 5
just use,
app:backgroundTint="@color/colorPrimary"
dont use,
android:backgroundTint="@color/colorPrimary"
Jjang
Updated on August 27, 2020Comments
-
Jjang over 3 years
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.
-
tachyonflux almost 9 yearsWell, the default color is not green. Where did you set a green color?
-
Jjang almost 9 yearsI did not. As you see, this xml is the only place I set up the layout of the button...
-
tachyonflux almost 9 yearsMaybe you should create a new project with just the FAB and post the code for the entire thing.
-
Jjang almost 9 yearsI 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.
-
tachyonflux almost 9 yearsWell, 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.
-
Jjang almost 9 yearsCould 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?
-
tachyonflux almost 9 yearsWow... 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. Thanks.
-
Jjang almost 9 yearsworks, but then I have to target api 21+. if I target api 8 can't call this method.
-
Vijet Badigannavar almost 9 yearsJust 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!!
-
Jjang almost 9 yearsof course I do, but this is the error on this code: Call requires API level 21 (current min is 8): android.widget.ImageView#setBackgroundTintList
-
Vijet Badigannavar almost 9 yearsThe 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!
-
Jjang almost 9 yearsOh 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!
-
Kaitlyn Hanrahan over 8 yearsThanks 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?
-
ywwynm over 8 years@KaitlynHanrahan Please see my update. Thanks for your comment.
-
Kaitlyn Hanrahan over 8 yearsThat 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.
-
hungryghost over 8 yearsThis doesn't work in Design Support Library v23 for < API 12 devices. It was working in v22, so they broke something, apparently.
-
Štarke over 8 yearsIs there a way how to set
app:borderWidth
programatically? Thanks -
Štarke over 8 yearsIs there a way how to set
app:borderWidth
programatically? Thanks -
ywwynm over 8 years@Š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 formBorderWidth
. -
Dantalian over 8 yearsNote that if you want to also change the image color to white for example you can use: android:tint="@android:color/white"
-
realappie over 8 yearsandroid:tint="@color/white" didnt work for me because it couldn't resolve it for some reason. android:tint="#ffffff" Works for me though
-
MBH over 8 yearshow will it be in the XML though ?
-
Dantalian over 8 yearsThat is because @color/white does not exists in your colors.xml. use @android:color/white or create a color named white or whatever you need
-
Vasil Valchev over 8 yearsuse "app:backgroundTint" not "android:backgroundTint"
-
Dinesh over 8 yearsThis work for normal colors. If I have given alpha color like #44000000 the frame color issue is reproducible.
-
Jason Cheladyn about 8 yearsYou must modify the theme you're using for that Activity in your AndroidManifest.xml. <activity android:name=".Activities.MainActivity" android:theme="@style/AppTheme" />
-
Bhargav Rao about 8 yearsWhilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
-
ywwynm about 8 years@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.
-
goelakash about 8 yearsOne 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).
-
Tinashe Chinyanga about 8 yearsgetResources.getColor() is deprecated, consider using ContextCompat.getColor(context, your colour); instead. Hope it helps
-
SMBiggs over 7 yearsHmm, 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 over 7 yearsWhen coloring the background, please be careful of the auto-complete and use
app:backgroundTint
and notandroid:tint
, which is the default. -
Shailendra Madda over 7 years
app:backgroundTint="@color/orange"
worked for me..Thanks -
jpact over 7 years@ScottBiggs use
android:theme="@style/AppTheme.FloatingAccentButtonOverlay"
but this solutin does not work for me either way... -
creativecreatorormaybenot over 7 yearsThis 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 theFAB
colour completely because it is not a solid color. You have to use theapp:
one to make it work. -
Kapenaar about 7 yearsThanks, 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 ;-) -
HenriqueMS about 7 years@Kapenaar no problem, was scratching my head for a while as well before noticing ;)
-
Dushyant Suthar about 7 yearsWhat about API 16 which does not have coloarAccent atrribute?
-
marienke almost 7 yearsHow would this work programatically since on the java side there is no difference like app: and android: in the XML.
-
HenriqueMS almost 7 years@marienke you can set it with a ColorStatesList, post a new question and post the link here so I can answer it ;)
-
KingKongCoder over 6 years
getResources().getColor(int id)
is deprecated now needsgetResources().getColor(int id, Theme theme)
-
IgorGanapolsky over 6 yearsThis is the easiest solution, way easier than using a custom drawable.
-
Kartik Chugh over 6 years@VasilValchev But the latter seems to work fine for me
-
ieselisra about 6 yearsThat's what I needed. Works with android.support.v7
-
ievgen over 5 yearsNot anymore, based on Theme Attribute Mapping the Floating Action Button now (I verified myself) is colored to colorSecondary (com.google.android.material:material:1.1.0-alpha02)
-
Yudi karma about 5 yearshmmmm this cause android studio autocomplete give result use android:backgroundTint , this answer verry good
-
Big_Chair about 5 yearsWow, the
app:borderWidth
is what's been off about my button! Setting it to 0 finally made it look like one clear color -
MainActivity over 4 yearsThe correct way. Tinting gives fill but tiny stroke of
colorAccent
remains around FAB. To use this needed to set theme for FAB:<style name="FabTheme" parent="Widget.MaterialComponents.FloatingActionButton"> <item name="colorAccent">@color/your_color</item> </style>
then addandroid:theme="@style/FabTheme"
. -
ManuelTS over 4 yearsFinally someone who provides simple solutions for FAB background and foreground (icon) colors.
-
TeachMeJava over 4 years
AAPT: error: duplicate attribute.
Just remove one ofapp:borderWidth="0dp"
-
Redar about 4 yearsThats the solution I was searchin for. I had a simple fab with backgrountTint and couldn't find a way to change it programmatically until I found this.
-
Mateus Melo almost 4 yearsThanks. That's what I was looking for
-
inspire_coding over 3 yearsI use Data Binding, and I got a NullPointerException...
-
jacoballenwood almost 3 yearsfor more info: material.io/develop/android/theming/color + material.io/design/color/…