Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active February 26, 2020 12:08
Show Gist options
  • Save webserveis/3237081671f28101e86d176437cf9dde to your computer and use it in GitHub Desktop.
Save webserveis/3237081671f28101e86d176437cf9dde to your computer and use it in GitHub Desktop.

Para cambiar el tinte de los iconos añadidos a un lado de un TextView

JAVA: Compatible Api < 23

private void setTextViewDrawableColor(@RecentlyNonNull TextView textView,@ColorRes int color) {
        for (Drawable drawable : textView.getCompoundDrawablesRelative()) {
            if (drawable != null) {
                drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(),color), PorterDuff.Mode.SRC_IN));
            }
        }
    }

Use

setTextViewDrawableColor(textView1,R.color.AccentColor)

KOTLIN

private fun setTextViewDrawableColor(@NonNull textView: TextView, @ColorRes color: Int) {
    for (drawable in textView.compoundDrawablesRelative) {
        if (drawable != null) {
            drawable.colorFilter = PorterDuffColorFilter(ContextCompat.getColor(textView.context, color), PorterDuff.Mode.SRC_IN)
        }
    }
}

Use

setTextViewDrawableColor(textView1,R.color.AccentColor)

KOTLIN EXTENSION

fun TextView.setDrawableColor(@ColorRes color: Int) {
    compoundDrawablesRelative.filterNotNull().forEach {
        it.colorFilter = PorterDuffColorFilter(ContextCompat.getColor(this.context, color), PorterDuff.Mode.SRC_IN)
    }
}

use

textView1.setDrawableColor(R.color.colorAccent)

ONLY XML API => 23

android:backgroundTintMode="src_in"
android:drawableTint="FF0000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment