0

I want the fonts of different devices to be exactly the same size, so I chose to use pt.

But with a lot of different resolution virtual devices try to find the same PT display font size is not the same

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.textView);
        String tx = "123456";
        tv.setText(tx);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, pt2px(this, 50));
        tv.setBackgroundColor(Color.RED);
        tv.setGravity(Gravity.CENTER);
    }

    public static int pt2px(Context context, float ptValue) {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        final float fontScale = dm.xdpi;
        return (int) (ptValue * (fontScale / 72f) + 0.5f);
    }
}

1

2

Thank you for answering!

I have found the reason, because the DPI obtained by DisplayMetrics is set by the device manufacturer itself, not necessarily accurate. The conversion between SP and PX requires scaledDensity, and this value is also inaccurate, so the font size of some devices obtained under the same SP is also different. https://rschilling.wordpress.com/2011/02/03/android-screen-density-inaccuracies/

But I still haven't found a solution.

  • check this link https://stackoverflow.com/questions/9877946/text-size-and-different-android-screen-sizes – Bunny Sep 09 '19 at 05:46

2 Answers2

0

Because your math is funky. First off- you're calculating the height (which is what textSize is) based on the horizontal dpi. It should be the vertical. Secondly, your math is reversed- dpi is dots (pixels) per inch. 50*dpi= the number of pixels in 50 inches. You aren't trying to make this 50 inches tall. Division would make sense unit-wise, but still probably isn't what you want. (I have no idea what you're actually trying to do).

You shouldn't be doing this anyway. Use SP. People set different font sizes based on their ability to easily read the phone. This is the only way people with vision issues can use their device. Work with them, and use SP so it scales with what they say they need.

Gabe Sechan
  • 77,740
  • 9
  • 79
  • 113
  • I just tried to use SP, but the size of the text on different devices is still different. – AndLightLight Sep 09 '19 at 07:13
  • If you're using SP and this it still will be. You should be using SP instead of this. At which point it may be different, if the user asked for it to be bigger or smaller. – Gabe Sechan Sep 09 '19 at 13:07
  • I found the reason, because some devices return the xdpi value is wrong, unlike my own manual calculation, such as Meizu X8, DisplayMetrics heightPixels is 2220, widthPixels is 1080, screen size is 6.2 inches, then calculated DPI should be around 398, but the densityDPI in DisplayMetrics is 480, xdpi is 318, ydpi is 320, all is wrong, you know what is the reason? – AndLightLight Sep 10 '19 at 02:07
  • Is this true in this link, the data obtained with DisplayMetrics may be incorrect?https://rschilling.wordpress.com/2011/02/03/android-screen-density-inaccuracies/ – AndLightLight Sep 10 '19 at 03:27
  • densityDPI isn't wrong, its a bucket that its mapped to which generally goes up in multiples of 160 (because 160 dpi is the base dpi of the original Android, it uses it as a multiplier of 1 for scaling. So this is a 3x device). I also don't know how you're doing your calculations, but I'm getting nowhere near 398. I got a number just around where they said it was, and the remainder is more likely to be mistakes in my quick math than theirs – Gabe Sechan Sep 10 '19 at 13:44
0

please use sp. As a person who has to use reading glasses to read facebook alerts, i'm begging you. You are going to get different results from different devices because of the aspect ratio among other things. pixel density doesn't have to be the same in each direction as you saw, and you are mixing terms. dpi is specifically how it will print. ppi is pixel density. Do not assume dpi has anything to do with your screen, because it doesn't. Not directly, anyway. If you're setting it on something you are going to display on the screen, it can and ususally does get scaled. This ESPECIALLY applies to text as there's a built-in setting in android to zoom (or shrink) it and any decent reader app will also let you pinch-zoom.

John Lord
  • 1,261
  • 6
  • 21
  • I have tried using SP, and the size obtained on some devices is different. I have found the reason, because the DPI obtained by DisplayMetrics is set by the device manufacturer itself, not necessarily accurate. The conversion between SP and PX requires scaledDensity, and this value is also inaccurate, so the font size of some devices obtained under the same SP is also different.https://rschilling.wordpress.com/2011/02/03/android-screen-density-inaccuracies/ – AndLightLight Sep 10 '19 at 06:27
  • you're quoting an 8 year old post. That was android 4. – John Lord Sep 10 '19 at 13:18