19

I'm working with FloatingActionButton. The user should be able to switch the FAB background color within a onClick Event.

However, the recommended call to setBackgroundTintList seems to be only compatible from 21+ API.

How do I - correctly - go about it on pre-lollipop devices? Is there any alternative I could use?

Thanks in advance.

Lampione
  • 1,622
  • 3
  • 21
  • 39

3 Answers3

34

You can use also setSupportBackgroundTintList

Applies a tint to the background drawable. Does not modify the current tint mode, which is SRC_IN by default.

Subsequent calls to View.setBackground(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode.

Also take a look on ViewCompat.setBackgroundTintList()

Applies a tint to the background drawable.

This will always take effect when running on API v21 or newer. When running on platforms previous to API v21, it will only take effect if view implement the TintableBackgroundView interface.

I found a solution here on SO that I've used before and is this:

public static void setButtonTint(Button button, ColorStateList tint) {
  if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
      ((AppCompatButton) button).setSupportBackgroundTintList(tint);
  } else {
      ViewCompat.setBackgroundTintList(button, tint);
  }
}

It works for me I hope it works for you too.

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Perfect! Just what I was looking for! Thank you – Lampione Nov 10 '15 at 22:15
  • Thanks upvote and mark it as a correct answer if it was what you needed :P Glad to hear that. – Skizo-ozᴉʞS ツ Nov 10 '15 at 22:17
  • 1
    But FloatingActionButton does not implement TintableBackgroundView, so the ViewCompat.setBackgroundTintList() does not work for API < 21 – lage May 31 '16 at 08:22
  • 2
    I get a lint warning when using AppCompatButton.setBackgroundTintList() AppCompatButton.setSupportBackgroundTintList can only be called from within the same library group (groupId=com.android.support) – starkej2 Apr 19 '17 at 18:11
  • @starkej2 may you post a question with images related with your issue and link here your question link? I'll try to help you :D – Skizo-ozᴉʞS ツ Apr 19 '17 at 20:34
  • @Skizo-ozᴉʞS I think all you need to do is call ViewCompat.setBackgroundTintList(button, tint) regardless of API level – starkej2 Apr 20 '17 at 14:03
  • ViewCompat.setBackgroundTintList(button, tint); is working for my AppCompatButton button's on API 19/Kit Kat. This answer's IF should really be checking for Lollipop and below. – Alan Jun 03 '17 at 20:41
10

simple:

fab.setBackgroundTintList(ColorStateList.valueOf(0xFF4CAF50));

fab is your FloatingActionButton of course and 0xFF4CAF50 just a example color

Rüdiger
  • 1,674
  • 1
  • 20
  • 28
0

From XML, you can use

card_view:backgroundTint="@color/your_color"

where card_view is xmlns:card_view="http://schemas.android.com/apk/res-auto"

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82