Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 296
fab Avatar asked Aug 17 '15 10:08

fab


4 Answers

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"
          />
like image 95
Farshid roohi Avatar answered Oct 06 '22 08:10

Farshid roohi


Drawable fabDr= mFAB.getDrawable();
DrawableCompat.setTint(fabDr, Color.WHITE);
like image 45
Jemshit Iskenderov Avatar answered Oct 06 '22 06:10

Jemshit Iskenderov


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);
like image 36
Rosário Pereira Fernandes Avatar answered Oct 06 '22 06:10

Rosário Pereira Fernandes


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));
like image 41
Chris Avatar answered Oct 06 '22 08:10

Chris