Change color of Floating Action Button from Appcompat 22.2.0 programmatically

52,388

Solution 1

Maybe late but could help.

 fab.setBackgroundTintList(ColorStateList.valueOf(Color
                    .parseColor("#33691E")));

and parse the actual color code from a list of colors You can find here

Solution 2

Create a ColorStateList and set it as the background tint:

button.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{color}));

Solution 3

To do this backwards compatible:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); // <- icon
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); // <- background

Solution 4

Create a color resource in colors.xml (R.color.purple in this case) and use it like so:

floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));

Solution 5

you have to use

  • in XML with attribute app:backgroundTint
  • in code with .setBackgroundTintList read this answer

Android changing Floating Action Button color

Share:
52,388
user2410644
Author by

user2410644

Updated on February 08, 2020

Comments

  • user2410644
    user2410644 over 4 years

    I would like to know how to change the Floating Action Button color from the Support library 22.2.0 ? I've tried

    button.setBackgroundColor(color);
    

    but clearly, this changes the drawable of the button and it turns to a square.

    Now I wonder how to change the color but just the color, without touching the shape?

    Thanks in advance