Drawable tinting for api <21

Android

Android Problem Overview


Is it possible to make drawable tinting work for api < 21?

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary" />

Works just fine but only for devices with API21. Any workaround for lower api devices or AppCompat support? Can't find anything.

Android Solutions


Solution 1 - Android

Use the AppCompatImageView like so:

<android.support.v7.widget.AppCompatImageView
        android:id="@+id/my_appcompat_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        android:tint="#636363"
    />

Make sure you have the latest appcompat-v7 in your app's build.gradle.

Example: compile 'com.android.support:appcompat-v7:25.0.0' in your app's build.gradle.

Solution 2 - Android

You can achieve that using source code. Previously tinting was not supported by DrawableCompat. Starting from support library 22.1 you can do that, but you need do it in this way:

Drawable normalDrawable = getResources().getDrawable(R.drawable.drawable_to_tint);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight));

Solution 3 - Android

Couldn't you simply use an ImageView to display your Drawable? android:tint works fine on older API levels.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_calendar"
    android:tint="@color/primary"
    />

Solution 4 - Android

A similar question has been asked before here: https://stackoverflow.com/a/26533340/950427

Android Drawable Tinting is supported in Android 5.0+ (API 21+) only. (It does say "At the moment this is limited to coloring the action bar and some widgets.").

> ## Theming > > ... > > When you set these attributes, AppCompat automatically propagates > their values to the framework attributes on API 21+. This > automatically colors the status bar and Overview (Recents) task entry. > > On older platforms, AppCompat emulates the color theming where > possible. At the moment this is limited to coloring the action bar and > some widgets.

And

> ## Widget tinting > > When running on devices with Android 5.0, all of the > widgets are tinted using the color theme attributes we just talked > about. There are two main features which allow this on Lollipop: > drawable tinting, and referencing theme attributes (of the form > ?attr/foo) in drawables. > > AppCompat provides similar behaviour on earlier versions of Android > for a subset of UI widgets: > > Everything provided by AppCompat’s toolbar (action modes, etc) > EditText Spinner CheckBox RadioButton Switch (use the new > android.support.v7.widget.SwitchCompat) CheckedTextView You don’t need > to do anything special to make these work, just use these controls in > your layouts as usual and AppCompat will do the rest (with some > caveats; see the FAQ below).

Sources:

http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

https://chris.banes.me/2014/10/17/appcompat-v21/

Solution 5 - Android

Now AppCompatImageView,AppCompatButton will replace the ImageView,Button to support tint on devices with lower API. Check link for more details AppCompatImageView,AppCompatButton

Solution 6 - Android

For tinting images you could use imageView.setColorFilter(int color). This works from API 8 and worked for tinting my black image to a color I wanted. This can replace setImageTintList() but just using android:tint should also work.

Solution 7 - Android

Use this NameSpace
xmlns:app="http://schemas.android.com/apk/res-auto"

and after you can replace every android:tint with app:tint. This fix the issue for me.

Solution 8 - Android

I'm a little late but here's how to do it.

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)
}

Solution 9 - Android

This will do as you want, and should work on all Android versions of the support library:

@JvmStatic
fun getTintedDrawable(inputDrawable: Drawable, @ColorInt color: Int): Drawable {
    val wrapDrawable = DrawableCompat.wrap(inputDrawable.mutate())
    DrawableCompat.setTint(wrapDrawable, color)
    DrawableCompat.setTintMode(wrapDrawable, Mode.SRC_IN)
    return wrapDrawable
}

Solution 10 - Android

If anyone want create new drawable (tin1,tint2..) try this

<?xml version="1.0" encoding="utf-8"?>
<bitmap
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:src="@drawable/your_image"
  android:tint="@color/tint_color">
   </bitmap>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaTToView Question on Stackoverflow
Solution 1 - AndroidSakiboyView Answer on Stackoverflow
Solution 2 - AndroidSimon K. GergesView Answer on Stackoverflow
Solution 3 - AndroidJonikView Answer on Stackoverflow
Solution 4 - AndroidJared BurrowsView Answer on Stackoverflow
Solution 5 - AndroidAnkit SinghView Answer on Stackoverflow
Solution 6 - AndroidPeterdkView Answer on Stackoverflow
Solution 7 - AndroidseintaView Answer on Stackoverflow
Solution 8 - AndroidCrazyView Answer on Stackoverflow
Solution 9 - Androidandroid developerView Answer on Stackoverflow
Solution 10 - AndroidRanjithkumarView Answer on Stackoverflow