Description:
I tried to change FAB color programmatically with this code
fabTest.backgroundTintList = ColorStateList.valueOf(toColor(R.color.mycolor))
It is working well when I set it from xml with this code
android:backgroundTint="@color/mycolor"
But I need to change the color programmatically.
I even tried to change Extended FAB color programmatically with the exact same code as above and it is working well.
Expected behavior:
Setting FAB color programmatically works.
Source code: The code snippet which is causing this issue. Please consider attaching a minimal sample app that reproduces the issue.
Android API version:
API 28
Material Library version:
1.3.0-alpha02 (I even tried with the latest stable version which is 1.2.1 and it is still not working)
Device:
Google Pixel
Tried without issues with 1.2.1 and latest 1.3.0:
fab.backgroundTintList = AppCompatResources.getColorStateList(this, R.color.xxxx)
where xxxx is a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="....." android:state_enabled="true"/>
<item android:alpha="0.12" android:color="...."/>
</selector>
What is your issue?
Just a note:
I even tried to change Extended FAB color programmatically with the exact same code as above and it is working well.
TheExtendedFloatingActionButton extends a MaterialButton and not aFloatingActionButton. They are different.
Hi Gabriele,
Thanks for your information. Unfortunately, I tried with your code and it is not changing my fab background color :(
My issue is I need to change the fab background color programmatically
But, it doesn't behave correctly.

E.g.
I need to change this FAB background color from Black to Blue, but I need to do it programmatically.
If you have a single color and not a ColorStateList have you tried with this?
fabTest.backgroundColor = ContextCompat.getColor(this, R.color.mycolor)
Hi matpag,
I have tried that too, and still no luck.
fabTest.setBackgroundColor(ContextCompat.getColor(context, R.color.myColor))
I suggest you to create a new fresh project with material theme and the FAB only, to test the various method we have suggested.
If the problem is still there after this, upload the fresh project on github and put the link here
Hi matpag,
I followed your suggestion and found something here.
I created a test project here
I added material dependencies only there.
After several tests, I found out that
setBackgroundColor is not working
I used this code, fabTest.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)).
So, in the end I used code which was suggested by Gabriele and it is working.
fabTest.backgroundTintList = AppCompatResources.getColorStateList(this, android.R.color.black)
fabTest.backgroundTintList = AppCompatResources.getColorStateList(this, R.color.fab_selector)
Both works.
android:backgroundTint in XML is the culprit
if I specify android:backgroundTint in XML then wanted to change color programmatically with
fabTest.backgroundTintList = AppCompatResources.getColorStateList(this, android.R.color.black)
FAB background color won't change. But if I take out android:backgroundTint in XML, then changing color programmatically works.
So, I solve this by not specifying android:backgroundTint in XML and set the initial color programmatically.
I'm thinking of this as a bug because normally we can set initial color in XML. Please have a look at my newly created test project.
Have you tried using app:backgroundTint to set the initial color instead?
Wow that worked.
Thanks a lot matpag
I'm closing this.
Most helpful comment
Hi matpag,
I followed your suggestion and found something here.
I created a test project here
I added material dependencies only there.
After several tests, I found out that
setBackgroundColoris not workingI used this code,
fabTest.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)).So, in the end I used code which was suggested by Gabriele and it is working.
fabTest.backgroundTintList = AppCompatResources.getColorStateList(this, android.R.color.black)fabTest.backgroundTintList = AppCompatResources.getColorStateList(this, R.color.fab_selector)Both works.
android:backgroundTintin XML is the culpritif I specify
android:backgroundTintin XML then wanted to change color programmatically withfabTest.backgroundTintList = AppCompatResources.getColorStateList(this, android.R.color.black)FAB background color won't change. But if I take out
android:backgroundTintin XML, then changing color programmatically works.So, I solve this by not specifying
android:backgroundTintin XML and set the initial color programmatically.I'm thinking of this as a bug because normally we can set initial color in XML. Please have a look at my newly created test project.