Search code examples
androidfloating-action-buttontint

Android tint icon in FloatingActionButton


How to tint an icon resource image in a FloatingActionButton? I've tried favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY); but no success.


Solution

  • You can set the color tint of the drawable like this if you are using API 21 or above.

    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));