Solution 1 :
I’m not quite sure what the problem is, but I’ve found a workaround which works for API 21.
view.getBackground().setColorFilter(tint_color, PorterDuff.Mode.SRC_OVER);
has the same effect as
view.setBackgroundTintList(ColorStateList.valueOf(tint_color));
Solution 2 :
I think it might be a problem with LinearLayout
try to use LinearLayoutCompat
to support older API.
<androidx.appcompat.widget.LinearLayoutCompat
android_layout_width="match_parent"
android_layout_height="match_parent"
android_background="@drawable/mybackground"
android_backgroundTint="#ff0000"
android_orientation="vertical">
<!--Sub views here-->
</androidx.appcompat.widget.LinearLayoutCompat>
What’s the difference between LinearLayout and LinearLayoutCompat Here You can read a little about differences
Problem :
I’ve been trying to apply a backgroundTint to a ConstraintLayout element having a Drawable background. However, the tint is not applied and the layout has the same background color as the drawable (only in API 21; works fine from API 23 upwards). It doesn’t work with LinearLayout and GridLayout either, so I think it might be something to do with ViewGroups. Here’s a simplified version of the element.
<LinearLayout
android_layout_height="match_parent"
android_layout_width="match_parent"
android_orientation="vertical"
android_background="@drawable/mybackground"
android_backgroundTint="#ff0000">
<!--Sub views here-->
</LinearLayout>
And here’s the background drawable.
<shape xmlns_android="http://schemas.android.com/apk/res/android">
<solid android_color="#ffffff"/>
<corners android_radius="10dp"/>
</shape>
How do I fix this?
Comments
Comment posted by clifordjoshy
This didn’t work. backgroundTint was introduced in API 21, so it can’t be a compatibility issue, right? Anyway, thanks for the help.
Comment posted by this
Yeah, maybe You just can’t use