How to get actual size of screen in Android Mode?

edited October 2017 in Android Mode


How can I access the device's xppi and yppi to make my app compatible with all devices?




  • println (displayWidth); 
    println (displayHeight); 
  • I am talking about actual screen size, as in - screen size in cm/inches. Some devices display way more pixels per inch than others, so scaling my app in pixels runs the risk of things being way too big or way too small in some devices. That's why I need access to my phone's PPI (pixels per inch).

  • Answer ✓


    see this code, easy to adapt to your needs:::

        import android.util.DisplayMetrics;
        float ts;
        void settings(){
        void setup(){
          DisplayMetrics metrics = new DisplayMetrics();    
        int screenDensity = metrics.densityDpi; 
        ts = screenDensity/160;
        void draw(){
          text("withoutscalingfactor", width/2, 300);
          text("essai", width/2, height/2);
  • @akenaton

    I can't describe how grateful I am for this snippet!

    One last thing: why do you divide the density by 160?

  • @randomdude===

    160 is for android the medium density reference.... ps: if it works, put answered for others...

  • edited October 2017

    Of course I will. I am very grateful for the work you went through to explain it, especially since I couldn't find it anywhere else on the net. I'm just unsure if after I mark it as answered I can pose my follow-up questions on here? :)

    What do you mean medium density reference?

    Isn't ts already the amount of pixels per inch? So, after I add this snippet, isn't ts/2.54 (1 inch is 2.54 cm) always the pixels of one centimeter, on any device? Why would I need to divide by 160?

    I'm sorry to bother you with more questions, I would just love it to clear things up to the max.

  • @ randomdude=== its simple yet rather long to explain as theses values are "android" defined in reference to 6 classes of phones. Same thing than yesterday: when i get some time free i ll explain as it is very important when coding for android to take in account these 6 classes.

  • @akenaton Alright mate, thanks for your effort! Can you just confirm that after importing this code, ts/2.54 is always 1 cm in pixels, on any device whatsoever?


    In case anybody's looking for any extra info on the matter.

Sign In or Register to comment.