Android Icon Icon in FloatingActionButton

How to paint an icon resource image in a FloatingActionButton? I tried favoriteFab.setColorFilter(R.color.yellow, PorterDuff.Mode.OVERLAY); but did not have time.

+7
source share
7 answers

You can set the color tone for drawing like this if you use API 21 or higher.

mFAB.getDrawable () mutate () setTint (GetResources () GetColor (R.color.yourColor).); ..

eg.

 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 is deprecated, you should use ContextCompat. Use the following, for example:

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

I assume favoriteFab is your FloatingActionButton. You can use:

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

You can simply use 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); 
+4
source
 Drawable fabDr= mFAB.getDrawable(); DrawableCompat.setTint(fabDr, Color.WHITE); 
+3
source

Instead, you can use DrawableCompat.setTintList() :

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

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) 
0
source

you 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" /> 
0
source

All Articles