D
D
Daniel Khaliulin2016-05-03 12:24:52
Java
Daniel Khaliulin, 2016-05-03 12:24:52

Why on android 5 the text size in Textview is so big?

I am debugging my app on different android versions. On all target versions my Textview looks good (all padding and size is right), but on Android 5 and 6 for some reason the Textview looks really big. Visually it seems that the size of the text itself is increasing. I tried to set the size both in sp and in dp, but the result is the same. I don't understand why, can you tell me please?
I create a textview like this:

TextView tv = new TextView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        Typeface type = Typeface.createFromAsset(getAssets(), "fonts/calibri.otf");
        tv.setTypeface(type);
        tv.setTextSize(getResources().getDimension(R.dimen.reader_main_text));
        tv.setLineSpacing(0.0f, 1.3f);
        ll.addView(tv);

In dimen, the reader_main_text parameter looks like this:
<dimen name="reader_main_text">11sp</dimen>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Gapchenko, 2016-05-03
@artemgapchenko

getResources().getDimension() returns the value in pixels, while setTextSize(float size) treats the parameter value as a number of scale-independent pixels by default and multiplies it by the appropriate factor again. Use a version of setTextSize() that allows you to set the unit to use.

tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.reader_main_text));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question