How to tint an icon resource image in a FloatingActionButton? I've tried favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY);
but no success.
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"
/>
Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);
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);
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With