Same text size but it larger on other devices

1.3k views Asked by At

I have a TextView in which I programmatically setup the text size in 'SP'.

tv.setTextSize(spToPx(5, context));

public static int spToPx(float sp, Context context) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}

First screenshot shows how it should look (and how it looks on my Xiaomi Redmi and Doogee HT7

First screenshot shows how it should look (and how it looks on my Xiaomi Redmi and Doogee HT7)

Second screenshot shows how it looks on other devices with higher or the same dpi.

Second screenshot shows how it looks on other devices with higher or the same dpi.

Normally, if I understand correct, text with the same size in 'dp' or 'sp' should looks the same on other devices (with the same screen size) because it automatically converts 'dp' or 'sp' to the needed amount of pixels depending on dpi.

After reading Android Developer Guideline about devices with different density I was expecting that text would be smaller if I use the same text size on devices with higher dpi. But now I have a situation that I really don't understand.

5

There are 5 answers

0
Oleg Golomoz On BEST ANSWER

I still didn't find right explanation to this issue. The only theory I have is next:

There are density buckets (120 dpi, 160 dpi, 240 dpi, 320 dpi, 480 dpi, 640 dpi). Screen density of your device will be determinated as the closest bucket. For example, if your device has 401 dpi it will be counted as 480 dpi. So, when I create and add view with size of 20 dp it will convert into more pixels than needed. It the hardest for devices that have density right between two buckets. I also checked density of all devices that had wrong image display (like on image 2) and it was almost right between two buckets...

I still don't know why this happens only when I create and add view dynamycally, but the only way to solve this problem was the creating of separate layout with all presetted views instead of adding these views dynamically.

4
sushildlh On

Try this, Add this property in your xml. It will change textsize based on all phone.

style="@android:style/TextAppearance.DeviceDefault.Medium" /*for normal text */
style="@android:style/TextAppearance.DeviceDefault.Small" /*for small text */
style="@android:style/TextAppearance.DeviceDefault.Large" /*for large text */

Hope your problem will solve.

1
Sotiris S. Magionas On

If you are using values in sp and the user changes the default text size for his device from Settings--->Display---->TextSize, your TextViews will be affected. If on the other hand you use dp values, then no matter what text size the user sets as default, the TextViews in your app will remain the same. This is ofcourse discouraged by Google as bad practice, since the user should be able to change text sizes but it would be what you are looking for.

So, use (TypedValue.COMPLEX_UNIT_DIP,number of your choosing) instead of (TypedValue.COMPLEX_UNIT_SP, sp)

1
Kevin Kurien On

You can add dimens file to your resources . thats how I do itenter image description here

heres the link where I got the answer

1
navylover On

The primary difference between an sp and a dp is that sp’s preserve a user's font settings. Users who have larger text settings for accessibility will see font sizes match their text size preferences.

so you could check the settings of two devices and see if there is a difference between them. enter image description here