To change icon color of Floating Action Button in Kotlin Android we have to set the tint attribute (in layout file) or imageTintList parameter (in Kotlin program) of FAB with the required color.
Add the floating action button to your layout The size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.
Using android:tint property you can set the color like this
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:tint="@android:color/white"
android:src="@android:drawable/ic_input_add"
/>
UPDATE 2
If you are using com.google.android.material.floatingactionbutton.FloatingActionButton
, use app:tint
app:tint="@android:color/white"
UPDATE
Refer to the answer of @Saleem Khan which is the standard way to set the app:tint
using:
android:tint="@android:color/white"
via XML on FloatingActionButton
.
OLD (June 2015)
This answer was written before October 2015, when android:tint
on FloatingActionButton
was supported only with API >= 21.
You can change it programmatically using ColorFilter
.
//get the drawable
Drawable myFabSrc = getResources().getDrawable(android.R.drawable.ic_input_add);
//copy it in a new one
Drawable willBeWhite = myFabSrc.getConstantState().newDrawable();
//set the color filter, you can use also Mode.SRC_ATOP
willBeWhite.mutate().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
//set it to your fab button initialized before
myFabName.setImageDrawable(willBeWhite);
If you are using Material Components
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
app:fabSize="normal"
app:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_google"/>
If you want to use icon default color, change app:tint="@null"
You have to change app:tint
for that to work. android:tint
didn't do any change for me.
It's easier than get the drawables, you only need to access to the color filter and set it to the color that you want.
FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.myfabid);
myFab.setColorFilter(Color.WHITE);
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