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

Rosário Pereira Fernandes
- 11,015
- 6
- 55
- 79

fab
- 790
- 6
- 10
-
3Why I'm getting -5, this is just a question?? – fab Aug 18 '15 at 14:19
-
1I was wondering that myself.. maybe lack of detail? I can understand the question though.. idk. – Nick H Sep 01 '15 at 18:42
7 Answers
9
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);

Rosário Pereira Fernandes
- 11,015
- 6
- 55
- 79
7
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));

Chris
- 514
- 1
- 5
- 8
-
2It only works on API > 21. Check my answer if you want to use it on lower APIs – Rosário Pereira Fernandes Jul 12 '16 at 14:05
4
Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);

Rosário Pereira Fernandes
- 11,015
- 6
- 55
- 79

Jemshit
- 9,501
- 5
- 69
- 106
4
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);

Filipe Bezerra de Sousa
- 2,874
- 1
- 17
- 17
3
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"
/>

Farshid roohi
- 722
- 9
- 23
2
You can use DrawableCompat.setTintList()
instead:
Drawable drawable = DrawableCompat.wrap(fab.getDrawable());
DrawableCompat.setTint(drawable, myColorInt);
fab.setImageDrawable(drawable);

Alex Lockwood
- 83,063
- 39
- 206
- 250
-
Always got a null pointer when getDrawable so needed to use your solution with something like Drawable drawable = DrawableCompat.wrap(getContext().getResources().getDrawable(R.mipmap.ic_help)); – lucblender Oct 29 '18 at 15:25
0
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)

Renetik
- 5,887
- 1
- 47
- 66