HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Android changing Floating Action Button color

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
actioncolorfloatingbuttonchangingandroid

Problem

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



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.

Solution

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






  • 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:


Code Snippets

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

Context

Stack Overflow Q#30969455, score: 1235

Revisions (0)

No revisions yet.