Is it possible to make drawable tinting work for api < 21?
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
}
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));
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.
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/
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>
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)
}