DrawableCompat setTint not working with API 19

I am using DrawableCompat to paint the tint, as shown below, it seems that the tinting does not work on API 19. I am using the 23.3.0 support library.

Drawable drawable = textView.getCompoundDrawables()[drawablePosition]; if (drawable != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { drawable.setTint(color); } else { DrawableCompat.setTint(DrawableCompat.wrap(drawable), color); } } 
+14
android android-support-library android-drawable
Apr 20 '16 at 0:47
source share
6 answers

I had the same problem. I merged the posts in https://stackoverflow.com/a/166269 and tried API 17, 19, 21, 22, 23 and N Preview 3 with SupportLib 23.4.0 to find a solution.

Even if it is mentioned that a compatible class will use a filter for pre-lollipop devices (see https://stackoverflow.com/a/115648/ ), it does not work.

Now I check the API myself and use the following code, which works with all the tested APIs (for 17 and above).

  // /questions/119964/android-tint-using-drawablecompat/735072#7350722170109 Drawable drawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.vector)); image.setImageDrawable(drawable); /* * need to use the filter | https://stackoverflow.com/a/30880522/2170109 * (even if compat should use it for pre-API21-devices | /questions/119964/android-tint-using-drawablecompat/735068#735068) */ int color = ContextCompat.getColor(context, R.color.yourcolor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { DrawableCompat.setTint(drawable, color); } else { drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); } 
+23
May 25 '16 at 10:20
source share

Works with API 15-25 using the AppCompat support library (verified 24.1.1 and higher).

 public static Drawable getTintedDrawable(@NonNull final Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) { Drawable d = ContextCompat.getDrawable(context, drawableRes); d = DrawableCompat.wrap(d); DrawableCompat.setTint(d.mutate(), ContextCompat.getColor(context, colorRes)); return d; } 
+13
Aug 23 '16 at 8:16
source share

I think that after wrapping your drawable you will need to call mutate() on it. See this: stack overflow

+1
Apr 20 '16 at 1:23
source share

based on @localhost answer

 Drawable d = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_rate)); DrawableCompat.setTint(d, Color.parseColor("#AAAAAA")); l.setLogo(d); 

I tried on API 19> 25 and it works well

0
Dec 24 '16 at 23:08
source share

I made the following helper method to set drawable with tint, and it works at least from API level 16 to x. The color is a thematic color and therefore resolved using the attribute.

  public static Drawable getTintedDrawable(int drawableResourceId, int colorAttribute) { YourApplication = YourApplication.getInstance(); Drawable drawable = ContextCompat.getDrawable(application, drawableResourceId); drawable = DrawableCompat.wrap(drawable); int tintColor = ContextCompat.getColor(application, application.getThemedResourceId(colorAttribute)); DrawableCompat.setTint(drawable, tintColor); return drawable; } 

Without drawable.wrap, it works on higher versions, but not at API level 16.

0
May 30 '18 at 8:25
source share

 val textInput = EditText(context) val drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable) drawable?.let { myDrawable -> DrawableCompat.setTint(myDrawable, ContextCompat.getColor(context, R.color.your_color)) textInput.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, myDrawable, null) } 
0
Jan 18 '19 at 14:57
source share



All Articles