How to change floating action button color?

Jeeva V picture Jeeva V · Jul 3, 2018 · Viewed 9.5k times · Source
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(PrivateDealActivity.this, R.color.colorWhite)));

This is not working. How to change background color of floating action button

Answer

hamster121 picture hamster121 · Jul 3, 2018

You can use this code to change the background color:

FloatingActionButton button = findViewById(R.id.your_button);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
    ((AppCompatButton) button).setSupportBackgroundTintList(ColorStateList.valueOf(*your color in integer*));
} else {
    ViewCompat.setBackgroundTintList(button, ColorStateList.valueOf(*your color in integer*));
}