singhspk singhspk - 1 year ago 95
Android Question

TextView.setTextSize behaves abnormally - How to set text size of textview dynamically for diffrent screens


is working abnormally. Right after the call to
if we get a
its returning a much higher value that what we set it to earlier.

Here's what we're doing:

zoomControl.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View view) {
float size = mViewShabad.getTextSize() + 1;

Has anyone seen this before?

Answer Source

The difference here is that in the setTextSize(int size) method, the unit type by default is "sp" or "scaled pixels". This value will be a different pixel dimension for each screen density (ldpi, mdpi, hdpi).

getTextSize(), on the other hand, returns the actual pixel dimensions of the text.

You can use setTextSize(int unit, float size) to specify a unit type. The constant values for this can be found in the TypedValue class, but some of them are:

TypedValue.COMPLEX_UNIT_PX   //Pixels

TypedValue.COMPLEX_UNIT_SP   //Scaled Pixels

TypedValue.COMPLEX_UNIT_DIP  //Device Independent Pixels
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download