Syed Arsalan Kazmi
Syed Arsalan Kazmi

Reputation: 1285

backgroundTint has not effect on lollipop version

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" />

Upvotes: 2

Views: 1222

Answers (2)

Ray Li
Ray Li

Reputation: 7919

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)

Upvotes: 2

Harshad
Harshad

Reputation: 1344

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

Upvotes: 1

Related Questions