[Floating Action Button] Changing color programmatically with backgroundTintList is not working
See original GitHub issueDescription:
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
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
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 specifyandroid:backgroundTint
in 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 outandroid: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.
Wow that worked. Thanks a lot matpag I’m closing this.