3
2

More than 3 years have passed since last update.

API23未満でTextViewにsetDrawableTintを使う

Last updated at Posted at 2019-11-08

はじめに

TextViewにアイコンを入れた際、色を付けるときにdrawableTintを使いたいですが、API23以降なので、minSDKによっては使えません。
その解決法と学びを記します。

コード

drawableStart/Endを利用している場合
@BindingAdapter("drawableTintCompat")
fun TextView.setDrawableTintCompat(color: Int) {
    compoundDrawablesRelative.forEach {
        it?.setColorFilter(color, PorterDuff.Mode.SRC_IN)
    }
}
drawableLeft/Rightを利用している場合
@BindingAdapter("drawableTintCompat")
fun TextView.setDrawableTintCompat(color: Int) {
    compoundDrawables.forEach {
        it?.setColorFilter(color, PorterDuff.Mode.SRC_IN)
    }
}
レイアウト(databindingが必要)
    <TextView
        app:drawableTintCompat="@{@color/hogehoge}"/>

ポイント

sdk21以上であれば、RTLの関係でdrawableStart/Endが推奨されています。
馴染みのあるgetCompoundDrawablesはStart/Endで指定していると取得できないことがあるため、getCompoundDrawablesRelativeを使いましょう。
https://developer.android.com/reference/android/widget/TextView.html#getCompoundDrawablesRelative()
また、Databindingを利用する関係で、ActivityやFragmentで使う際はDatabindingUtilでinflateやbindをしてレイアウトを設定する必要があります。
(xmlをで囲んだだけではbindされないため、そのままsetContentViewとかをしてもdrawableTintCompatは動きません)

おまけ

塗る処理で、setColorFilterではなくsetTintを使う場合は注意が必要です。

@BindingAdapter("drawableTintCompat")
fun TextView.setDrawableTintCompat(color: Int) {
    compoundDrawablesRelative.forEach {
        it?.apply {
            setTint(color)
            setTintMode(PorterDuff.Mode.SRC_IN)
        }
    }
}

この場合、drawableに対して直接Tintをしているので、他で使っている同じdrawableリソースの色も変わってしまいます。
色が単色の場合・毎回定義している場合は問題ないですが、そうでない場合はmutateを呼んで上げる必要があります。

@BindingAdapter("drawableTintCompat")
fun TextView.setDrawableTintCompat(color: Int) {
    compoundDrawablesRelative.forEach {
        it?.mutate()?.apply {
            setTint(color)
            setTintMode(PorterDuff.Mode.SRC_IN)
        }
    }
}

mutateについては、以下の記事に詳しく書かれています。
http://y-anz-m.blogspot.com/2010/10/androiddrawable-mutations.html

最後に

drawablesがnullになってるのはなんでだろう・・・。
と思っていたらまさか別の取得方法があったとは。
compoundDrawablesRelative、忘れないでしょう。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2