How to get actual size of screen in Android Mode?

edited October 3 in Android Mode

Hello!

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

Thanks!

Tagged:

Answers

  • 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 ✓

    @randomdude===

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

        import android.util.DisplayMetrics;
    
    
        float ts;
    
    
        void settings(){
          fullScreen();
        }
    
        void setup(){
    
          background(255);
          fill(0);
          //textSize(48);
    
          DisplayMetrics metrics = new DisplayMetrics();    
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);    
        int screenDensity = metrics.densityDpi; 
        println(screenDensity);
    
        ts = screenDensity/160;
        println(ts);
    
        }
    
    
        void draw(){
          background(255);
          textSize(24);
          text("withoutscalingfactor", width/2, 300);
    
          textSize(24*ts);
          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 3

    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?

  • https://developer.android.com/guide/practices/screens_support.html

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

Sign In or Register to comment.