Search code examples
androidandroid-layoutfloating-action-buttonmaterial-components-android

Unable to change icon color in layout file for a FloatingActionButton on Android Studio


So, I have this layout file with the following skeleton:

<layout>
  <CoordinatorLayout>
    <AppBarLayout>
    ... some tags and stuff ...
    </AppBarLayout>
    <NestedScrollView>
    .... nested scroll view stuff
    </NestedScrollView>
    <!-- Finally! Here comes.... -->
    <FloatingActionButton>
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="8dp"
        android:backgroundTint="@color/someDarkBlueColor"
        android:tint="@color/colorWhite"
        android:src="@drawable/ic_white_camera" />
        ... closing tags and stuff ....

ic_white_camera is as follows:

<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="200dp" android:height="200dp" android:viewportWidth="24.0" android:viewportHeight="24.0">
    <path android:fillColor="@color/colorWhite" android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
    <path android:fillColor="@color/colorWhite" android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>

@color/colorWhite is.. You guessed. White...

However android insists on rendering the camera icon in black!

I've tried adding android:tint="@color/colorWhite". No go..

Added android:foregroundTint="@color/colorWhite" and... Nada!

Still getting this:

Pesky camera in black

Any clues on this one?

Best regards to y'all.. :-)


Solution

  • If you use the FloatingActionButton that in com.google.android.material package, you should add app:tint instead of android:tint. Then in your xml you should be using app:tint="@color/colorWhite" instead of using android:tint="@color/colorWhite".