Yegie
Yegie

Reputation: 61

Android set one TextView's textsize equal to another's

So I have a textview who's size is being set based on android's autoSizeTextType="uniform". I want to copy that size to two other text views.

Here is what I have tried:

    val firstButton = activity?.findViewById<TextView>(R.id.b1)
    val secondButton = activity?.findViewById<TextView>(R.id.b2)
    val thirdButton = activity?.findViewById<TextView>(R.id.b3)

    if (firstButton != null) {
        secondButton?.textSize = firstButton.textSize
        thirdButton?.textSize = firstButton.textSize
    }

This, however, results in the text size of 2 and 3 being about 4x as large as the first. Anyone have any ideas as to what I am doing wrong? I'm guessing it is getting some sort of dpi scaled size, but I'm not sure how to get the correct size.

Upvotes: 1

Views: 100

Answers (1)

Sergey Glotov
Sergey Glotov

Reputation: 20356

TextView.getTextSize() returns size in pixels, but setTextSize() interpret parameter as scaled pixels (sp). There's setTextSize() overload which accept text size and unit. In your case unit should be TypedValue.COMPLEX_UNIT_PX.

secondButton?.setTextSize(TypedValue.COMPLEX_UNIT_PX, firstButton.textSize)

Upvotes: 1

Related Questions