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()
}