user19862929
user19862929

Reputation:

Setting Height and Width of textView based on the text

I have a textView function that's create text view

fun createTextView(text: String, index: Int): TextView

Now I'm trying to make the textView height and width based on the text I input


val words = arrayListOf("abc")

I tried 

for ((wordIndex, i) in words.withIndex()){
            val textView = createTextView(i,wordIndex)
            textView.y = shuffleXList[wordIndex]*83F
            textView.x=  shuffleYList[wordIndex]*83F
            textView.measure(0, 0)
            textView.height = textView.measuredHeight
            textView.width = textView.measuredWidth
            textView.setBackgroundColor(BgColor)
}

This is what I got enter image description here

how do I adjust the weight and height such that the color is only around the text?

Upvotes: 0

Views: 102

Answers (1)

Hitesh Khatri
Hitesh Khatri

Reputation: 91

You have to try this one

textView.setText("abc");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();

Upvotes: 1

Related Questions