question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Floating Action Button] Changing color programmatically with backgroundTintList is not working

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
franzandelcommented, Sep 19, 2020

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

  1. 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.

  2. 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.

0reactions
franzandelcommented, Sep 19, 2020

Wow that worked. Thanks a lot matpag I’m closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android changing Floating Action Button color - Stack Overflow
As described in the documentation, by default it takes the color set in styles.xml attribute colorAccent. The background color of this view defaults...
Read more >
How to Change Background Color of Floating Action Button
To change background color of Floating Action Button in Kotlin Android, set the backgroundTint attribute (in layout file) or backgroundTintList property (in ...
Read more >
How to Change Colors of a Floating Action Button in Android?
Step 1: Create a New Project in Android Studio · Step 2: Working with the activity_main.xml file · Step 3: Working with the...
Read more >
backgroundTintList does not work properly when set ...
Programmatically setting the backgroundTintList of a FloatingActionButton does ... despite being in the correct states, the control does not change color.
Read more >
Add a Floating Action Button - Android Developers
Add the floating action button to your layout · The size of the FAB, using the app:fabSize attribute or the setSize() method. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found