1

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.

Community
  • 1
  • 1
Oleg Golomoz
  • 502
  • 4
  • 12

5 Answers5

1

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)

0

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.

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • Thank you for the answer. But what exactly does it mean? I really want to understand the situation at pixel-level at least :) – Oleg Golomoz Nov 21 '18 at 11:49
  • It will use Medium text size as per all device. Don't use textSize property with this. – sushildlh Nov 21 '18 at 11:53
  • But somewhere I use large text, somewhere small and somewhere medium like on the screenshots. And in all cases other devices have larger text than it should. If I will use your answer it will setup all TextViews with the same text size? – Oleg Golomoz Nov 21 '18 at 12:02
  • use `TextAppearance.DeviceDefault.Small` for small textSize and `TextAppearance.DeviceDefault.Large` for large textSize. – sushildlh Nov 21 '18 at 12:05
0

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

heres the link where I got the answer

Kevin Kurien
  • 812
  • 6
  • 14
  • But there is a problem that dpi is higher and the text looks larger also. But it shouldn't be like that. References to Android Guideline and your answer the text should be smaller that is why we have to create dimens file and define there more 'sp' for higher dpi. – Oleg Golomoz Nov 21 '18 at 12:00
0

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

navylover
  • 12,383
  • 5
  • 28
  • 41
  • I tried 'dp' before and had the same effect. Also, if app users have different text size settings what can I do as developer to create the same text size for all? – Oleg Golomoz Nov 21 '18 at 12:56
0

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.

Oleg Golomoz
  • 502
  • 4
  • 12