Reputation: 3488
Aim :
MyDrawable
which extends to GradientDrawable
.MyDrawable
.res->drawable->test_drawable.xml
which has my <drawable>
resource.test_drawable.xml
to pass values to MyDrawable
.test_drawable.xml
in MyDrawable.inflate()
this part is not workingProblem :
In MyDrawable.inflate()
I am not getting my custom attribute values defined in res->drawable->test_drawable.xml
MyDrawable:
class MyDrawable() : GradientDrawable() {
private var radiusPercentage : Int = 25
override fun inflate(
r: Resources,
parser: XmlPullParser,
attrs: AttributeSet,
theme: Resources.Theme?
) {
super.inflate(r, parser, attrs, theme)
val a = r.obtainAttributes(attrs, R.styleable.MyDrawable)
// I am always getting 0 here. Checked with breakpoint in debug mode.
val radius = a.getInteger(R.styleable.MyDrawable_radiusPercentage, 0)
radiusPercentage = radius
}
}
Custom attribute:
<declare-styleable name="MyDrawable">
<attr name="radiusPercentage" format="integer"/>
</declare-styleable>
res->drawable->test_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="com.test.app.RoundedCornersDrawable
app:radiusPercentage="50" />
I am setting it on my container
binding.container.background =
AppCompatResources.getDrawable(context, R.drawable.test_drawable)
Upvotes: 1
Views: 229