How to read dimension values from custom attribute?

579 views Asked by At

I have custom Button which is completely made up by my own xml. I can set up style, color and text + icon to it, but now I need to set custom text size.

I added to my styleable attributes new attribute <attr name="l_buttonTextSize" format="dimension"/> then I set value for textSize from dimensions app:l_buttonTextSize="@dimen/text_small". But I cant read it in button initialization.

This is how I read that value:

val buttonTxtSize = typedArray.getFloat(btnTextSizeIndex, resources.getDimension(R.dimen.text_medium))
setButtonTextSize(buttonTxtSize)

fun setButtonTextSize(value: Float){
    buttonText.setTextSize(TypedValue.COMPLEX_UNIT_PX, value)
}

I've got this exception:

java.lang.NumberFormatException: For input string: "12.0sp"

Looks like string was send instead of dimension value as float.

1

There are 1 answers

0
Subhrajyoti Sen On

You can use getDimensionPixelSize

val buttonTxtSize = typedArray.getDimensionPixelSize(btnTextSizeIndex, resources.getDimension(R.dimen.text_medium))