View extension methods to tint current background

This commit is contained in:
Maxime Naturel 2022-03-03 10:58:56 +01:00
parent 7cad30e6bb
commit 9b6811c915
2 changed files with 14 additions and 13 deletions

View file

@ -23,6 +23,7 @@ import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
@ -70,6 +71,15 @@ fun View.setAttributeTintedBackground(@DrawableRes drawableRes: Int, @AttrRes ti
background = drawable
}
fun View.tintBackground(@ColorInt tintColor: Int) {
val bkg = background?.let {
val backgroundDrawable = DrawableCompat.wrap(background)
DrawableCompat.setTint(backgroundDrawable, tintColor)
backgroundDrawable
}
background = bkg
}
fun ImageView.setAttributeTintedImageResource(@DrawableRes drawableRes: Int, @AttrRes tint: Int) {
val drawable = ContextCompat.getDrawable(context, drawableRes)!!
DrawableCompat.setTint(drawable, ThemeUtils.getColor(context, tint))

View file

@ -24,9 +24,9 @@ import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.setPadding
import im.vector.app.R
import im.vector.app.core.extensions.tintBackground
import im.vector.app.databinding.ViewLocationSharingOptionBinding
/**
@ -61,12 +61,7 @@ class LocationSharingOptionView @JvmOverloads constructor(
}
fun setIconBackgroundTint(@ColorInt color: Int) {
val bkg = binding.shareLocationOptionIcon.background?.let {
val backgroundDrawable = DrawableCompat.wrap(binding.shareLocationOptionIcon.background)
DrawableCompat.setTint(backgroundDrawable, color)
backgroundDrawable
}
binding.shareLocationOptionIcon.background = bkg
binding.shareLocationOptionIcon.tintBackground(color)
}
private fun setIcon(typedArray: TypedArray) {
@ -83,12 +78,8 @@ class LocationSharingOptionView @JvmOverloads constructor(
val description = typedArray.getString(R.styleable.LocationSharingOptionView_iconDescription)
iconView.setImageDrawable(icon)
val bkg = background?.let {
val backgroundDrawable = DrawableCompat.wrap(it)
DrawableCompat.setTint(backgroundDrawable, backgroundTint)
backgroundDrawable
}
iconView.background = bkg
iconView.background = background
iconView.tintBackground(backgroundTint)
iconView.setPadding(padding)
iconView.contentDescription = description
}