android / android-ktx

A set of Kotlin extensions for Android app development.
https://android.github.io/android-ktx/core-ktx/
7.47k stars 563 forks source link

Resources.Theme.resolveAttribute extension #587

Open romtsn opened 6 years ago

romtsn commented 6 years ago

To facilitate the retrieving of an attribute from a theme, I'd like to propose the following extension:

inline fun Resources.Theme.resolveAttribute(
    resId: Int,
    resolveRefs: Boolean = true
): TypedValue = TypedValue().apply { resolveAttribute(resId, this, resolveRefs) }

So this will lead to the following change:

// before
val backgroundValue = TypedValue() 
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)

// after
val backgroundValue = context.theme.resolveAttribute(android.R.attr.selectableItemBackground)