Android Get Drawable From Attribute (R.attr)

January 31, 2020
fun Context.getDrawableFromAttribute(attributeId: Int): Drawable? {
    val typedValue = TypedValue().also { theme.resolveAttribute(attributeId, it, true) }
    return  ContextCompat.getDrawable(this, typedValue.resourceId)
}

Usage

toolbar.navigationIcon = context.getDrawableFromAttribute(R.attr.homeAsUpIndicator)
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.