0

I have an attributes file in my resources where i store a bunch of different colours, and have been settings them in xml like so:

android:textColor="?attr/textcolor"

Say I have a TextView called textView in my Java. How can I do the same task programmatically?

I tried with this

fun fetchAttr(@AttrRes attr: Int, vararg args: Any?): Int {
    val typedArray = context.obtainStyledAttributes(intArrayOf(attr))
    val textColor = typedArray.getColor(0, 0)
    typedArray.recycle()
    return textColor
}

textview.fetchAttr(android.R.attr.black)

but when i use my attr uiColorPrimaryRipple in my attrTest.xml like this :

<attr name="uiColorPrimaryRipple" format="color" />

and i put :

    textview.fetchAttr(R.attr.uiColorPrimaryRipple)

does not work for me

0 Answers0