Nathan Fig Nathan Fig - 2 years ago 220
Android Question

Android TextView setTextSize incorrectly increases text size

This is in an extension of TextView.

are not overridden, I do not extend those methods. Programming in 1.6, API level 4.

The loop in this code causes size to be multiplied by 1.5 every time it iterates, e.g. if size initially reads 200 from
, then
is called,
called again reads back 300.

public void shrinkTest() {
float size = this.getTextSize();
while (size > 8) {
size = this.getTextSize();

Why is this?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download