Skip to content

Instantly share code, notes, and snippets.

@L-Briand
Created February 18, 2022 16:28
Show Gist options
  • Save L-Briand/a6a78d69f739ac29c1d2ba6cd8de3aed to your computer and use it in GitHub Desktop.
Save L-Briand/a6a78d69f739ac29c1d2ba6cd8de3aed to your computer and use it in GitHub Desktop.
Android: Resolve drawable theme attribute from context
inline fun <reified T> TypedArray.autoRecycle(block: TypedArray.() -> T): T {
val result = block()
recycle()
return result
}
fun resolveDrawable(context: Context, @AttrRes attr: Int): Drawable? =
context.theme.obtainStyledAttributes(intArrayOf(attr)).autoRecycle { getDrawable(0) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment