Android TextView setTextSize incorrectly increases text size

<>

This question already has an answer here:

Answers


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.


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


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)


Need Your Help

How do I call an Angular.js filter with multiple arguments?

angularjs

As from the documentation, we can call a filter such as date like this:

Best way to compare XElement objects

.net xml linq unit-testing

in a unit test I'm comparing an XElement object with the one I expect. The method I use is to invoke .ToString() on the XElement object and compare it with a hard-coded string value. This method tu...