Skip to content Skip to sidebar Skip to footer

Android TextView SetTextSize Incorrectly Increases Text Size

This is in an extension of TextView. getTextSize() and setTextSize() are not overridden, I do not extend those methods. Programming in 1.6, API level 4. The loop in this code cause

Solution 1:

Heh, mixed units problem. Seems a little counterintuitive, but it's an easy fix. The default method setTextSize(float) assumes you're inputting sp units (scaled pixels), while the getTextSize() method returns an exact pixel size.

You can fix this by using the alternate setTextSize(TypedValue, float), like so:

this.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

This will make sure you're working with the same units.


Solution 2:

setTextSize() and getTextSize() work with different units. The parameter to set() is density-independent "scaled pixels", whereas get() returns plain old pixels.


Solution 3:

pass units with size using TypedValue like below:

TypedValue.COMPLEX_UNIT_PX //Pixels

TypedValue.COMPLEX_UNIT_SP //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels

setTextSize(TypedValue.COMPLEX_UNIT_SP, 18)


Post a Comment for "Android TextView SetTextSize Incorrectly Increases Text Size"