Android changing Floating Action Button color
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" />
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.
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"
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)));