Home » Android » floating action button – Android tint icon in FloatingActionButton

floating action button – Android tint icon in FloatingActionButton

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

I’m assuming favoriteFab is your FloatingActionButton. You can use:

int color = ContextCompat.getColor(this, R.color.yellow);
favoriteFab.getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);

Answer:

You can simple use the DrawableCompat in support-v4 as follows:

    Drawable drawable = mFloatingActionButton.getDrawable();
    // Wrap the drawable so that future tinting calls work
    // on pre-v21 devices. Always use the returned drawable.
    drawable = DrawableCompat.wrap(drawable);

    // We can now set a tint
    DrawableCompat.setTint(drawable, ContextCompat.getColor(this, R.color.white));
    // ...or a tint list
    DrawableCompat.setTintList(drawable, ColorStateList.valueOf(ContextCompat.getColor(this, R.color.white)));

    // ...and a different tint mode
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

Answer:

Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);

Answer:

You can use DrawableCompat.setTintList() instead:

Drawable drawable = DrawableCompat.wrap(fab.getDrawable());
DrawableCompat.setTint(drawable, myColorInt);
fab.setImageDrawable(drawable);

Answer:

can use :

app:tint="#FFFF"       

example :

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="16dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            app:backgroundTint="@color/colorAccent"
            app:fabSize="normal"
            app:rippleColor="@color/colorPrimaryDark"
            app:tint="#FFFF"
            android:src="@drawable/ic_search_24dp"
          />

Answer:

Or nicer in Kotlin

import androidx.core.graphics.drawable.DrawableCompat.setTint
import com.google.android.material.floatingactionbutton.FloatingActionButton

fun FloatingActionButton.iconTint(color: Int) = setTint(drawable, color)