2

I have set the the backgroundTint of a view as shown below but it has no effect on its color. On xml design view, it looks fine but not on the device. I have lollipop's version on my device.

compileSdkVersion and targetSdkVersion is 21.

<View
   android:layout_width="35dp"
   android:layout_height="35dp"                     
   android:background="@drawable/circle_appointment_statuses"
   android:backgroundTint="@color/pending" />
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
  • Possible duplicate of [Lollipop's backgroundTint has no effect on a Button](http://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button) – Harshad Feb 16 '16 at 12:45
  • No, In that case its a button and it was handled by overriding an attribute "android:colorButtonNormal" in the theme. But in this case its a View. – Syed Arsalan Kazmi Feb 16 '16 at 12:55

2 Answers2

2

To tint a background drawable on a View on Lollipop (API 21), use setColorFilter(int color, PorterDuff.Mode mode).

layout.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.color), PorterDuff.Mode.SCR_ATOP)

The following methods DO NOT work on background drawables on Lollipop.

background.setTint(int color)

DrawableCompat.setTint(Drawable drawable, int color)

Ray Li
  • 7,601
  • 6
  • 25
  • 41
1

The bad news

it's meaningless to tint a Button's background in Lollipop 5.0 (API level 21).

The good news

Lollipop 5.1 (API level 22) seems to have fixed this by changing btn_mtrl_default_shape.xml (among other files): https://android.googlesource.com/platform/frameworks/base/+/6dfa60f33ca6018959ebff1efde82db7d2aed1e3%5E!/#F0

The great news

The new support library (version 22.1+) adds backward-compatible tinting support to lots of components, including AppCompatButton!

Unfortunately, the android:backgroundTint property still doesn't work (maybe I'm doing something wrong) -- so you have to set the ColorStateList in code, using setSupportBackgroundTintList(). It'd be really nice to see android:backgroundTint supported in the future.

More Detail visit here.Lollipop's backgroundTint has no effect on a Button

Community
  • 1
  • 1
Harshad
  • 1,344
  • 1
  • 10
  • 25