Apply tint list for preference icons

This commit is contained in:
Moez Bhatti
2019-11-19 02:37:19 -05:00
parent 8fd888744a
commit 77b14e6557
3 changed files with 19 additions and 2 deletions
@@ -19,6 +19,7 @@
package com.moez.QKSMS.common.util.extensions
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.TypedValue
import android.widget.Toast
@@ -31,6 +32,12 @@ fun Context.getColorCompat(colorRes: Int): Int {
return tryOrNull { ContextCompat.getColor(this, colorRes) } ?: Color.BLACK
}
fun Context.getColorStateListCompat(colorStateListRes: Int): ColorStateList {
//return black as a default color, in case an invalid color ID was passed in
return tryOrNull { ContextCompat.getColorStateList(this, colorStateListRes) }
?: ColorStateList.valueOf(Color.BLACK)
}
/**
* Tries to resolve a resource id from the current theme, based on the [attributeId]
*/
@@ -55,6 +62,14 @@ fun Context.resolveThemeColor(attributeId: Int, default: Int = 0): Int {
return if (wasResolved) getColorCompat(outValue.resourceId) else default
}
fun Context.resolveThemeColorStateList(attributeId: Int, default: Int = 0): ColorStateList {
val outValue = TypedValue()
val wasResolved = theme.resolveAttribute(attributeId, outValue, true)
return getColorStateListCompat(if (wasResolved) outValue.resourceId else default)
}
fun Context.makeToast(@StringRes res: Int, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, res, duration).show()
}
@@ -26,6 +26,7 @@ import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.extensions.resolveThemeAttribute
import com.moez.QKSMS.common.util.extensions.resolveThemeColorStateList
import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.injection.appComponent
import kotlinx.android.synthetic.main.preference_view.view.*
@@ -71,7 +72,9 @@ class PreferenceView @JvmOverloads constructor(
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
context.obtainStyledAttributes(attrs, R.styleable.PreferenceView)?.run {
icon.imageTintList = context.resolveThemeColorStateList(android.R.attr.textColorSecondary)
context.obtainStyledAttributes(attrs, R.styleable.PreferenceView).run {
title = getString(R.styleable.PreferenceView_title)
summary = getString(R.styleable.PreferenceView_summary)
@@ -32,7 +32,6 @@
android:layout_height="24dp"
android:layout_marginStart="24dp"
android:duplicateParentState="true"
android:tint="?android:attr/textColorSecondary"
android:visibility="invisible" />
<LinearLayout