2

From the sample in the SDK for floating action buttons I have a drawable:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
    <ripple android:color="@color/primary_light">
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/primary" />
            </shape>
        </item>
    </ripple>
</item>

<item>
    <ripple android:color="@color/primary_light">
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/primary" />
            </shape>
        </item>
    </ripple>
</item>
</selector>

and I'm setting my TextView background:

android:background="@drawable/fab_background"

Is there a way that I can access fab_background or edit my TextView background itself to change the color of my FAB?

6
  • Why cant you just change the <solid android:color="@color/primary" /> Commented Mar 17, 2015 at 23:31
  • How do I do that dynamically though? Can I reference a drawable in java code and access that attribute? Commented Mar 17, 2015 at 23:40
  • I dont think thats so easy. You could call getDrawable and then something like applyColorFilter Commented Mar 17, 2015 at 23:53
  • What API levels are you targeting?
    – alanv
    Commented Mar 17, 2015 at 23:56
  • @alanv I'm only targeting 5.0+ Commented Mar 17, 2015 at 23:58

3 Answers 3

6

You can change color of FAB by setting the following attribute of <android.support.design.widget.FloatingActionButton> in the layout xml:

app:backgroundTint="#ff0000"
0

If you're targeting API 21+, you can take advantage of drawable tinting and drawable color state lists by specifying your base drawable as @android:color/white and applying a color state list as a tint.

You can also optionally take advantage of drawable themeing if you'd like to use the platform-default ripple color. I've used this in the example below.

res/values/colors.xml:

<resources>
  <color name="fab_checked">...</color>
  <color name="fab_normal">...</color>
  ...
</resources>

res/color/fab_tint.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true"
        android:color="@color/fab_checked" />
  <item android:color="@color/fab_normal" />
</selector>

res/drawable/fab.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
    <item>
        <shape android:shape="oval"
               android:tint="@color/fab_tint">
            <solid android:color="@color/white" />
        </shape>
    </item>
</ripple>

If you want to change the tint dynamically, you can just call Drawable.setTint(int) or Drawable.setTintList(ColorStateList).

myFab.getDrawable().mutate().setTint(Color.RED);

If you want to preserve the checked state color, you can create a new ColorStateList and apply that.

ColorStateList csl = new ColorStateList(
    new int[][] {{android.R.attr.state_checked},{}},
    new int[] {Color.GREEN, Color.RED});
myFab.getDrawable().mutate().setTintList(csl);
0

You can simply try this code ,worked for me

            app:backgroundTint="@color/colorPrimaryDark"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.