fab fab - 4 months ago 29
Android Question

Android tint icon in FloatingActionButton

How to tint an icon resource image used with FloatingActionButton, I tried

favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY);
but no success.

Answer

You can set the color tint of the drawable like this.

mFAB.getDrawable().mutate().setTint(getResources().getColor(R.color.yourColor));

E.g.

mFAB = (FloatingActionButton) findViewById(R.id.fab);
mFAB.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar.make(v, "Yummy snackbar", LENGHT_LONG).show();
    }
});
mFAB.getDrawable().mutate().setTint(getResources().getColor(R.color.colorAccent));

Update: Since getColor has been deprecated you should use ContextCompat instead. Use the following e.g:

mFAB.getDrawable().mutate().setTint(ContextCompat.getColor(this, R.color.colorAccent));