I have created one CustomView class where I want to get drawable dynamically. So for that have created attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="myImage" format="reference"/>
</declare-styleable>
</resources>
And then set this attrs through xml file like below:
<com.example.myapplication.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:myImage="@drawable/my_icon"/>
Now I want to get this myImage
value in my CustomView
class how can i get it?
I have already tried many ways to get it by TypedValue and TypedArray but not able to get it.
val typedValue = TypedValue()
context.theme.resolveAttribute(R.attr.myImage, typedValue, true)
val imageResId = ContextCompat.getDrawable(context,typedValue.resourceId)
val typedArray =
context.theme.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
val imageResId = typedArray.getResourceId(R.styleable.CustomView_myImage,0)