Different sizes on different devices

edited April 2018 in Android Mode

Hi all! I hope you're having a good day.

I recently made and published a small game on the Google Play Store, but I found that it appears different on different devices. I've tried to debug and find out what's causing the problem and I've narrowed it down to different devices having different pixel densities, since I hardcoded values for the sizes of all my objects. I tried dividing these hardcoded values with displayDensity but the objects are still far too large. How can I go about standardising the appearance across devices?

Also, to avoid this inter-device incompatibility in the future, how should I be structuring my code instead?

Thank you all!



  • Answer ✓

    Check these links:


    By the way, not sure if pixelDensity works in Android Processing. In Processing Java, pixelDensity was used to address retina display in mac units. However, the Windows counterpart has not been implemented as yet. I don't ahve experience in the Android side, so you would need to test it yourself or diving into the source code to make sure it is doing what it suppose to do.


  • edited April 2018

    My code might be badly written but I somehow managed to get it to work - same object sizes regardless of pixel density. For reasons unknown displayDensity returns your actual pixel density divided by 160. So I put the following code in draw: scale(displayDensity / 3.0), since I had originally optimised the shapes for a 480 dpi screen, hence the 3.0. After that I multiplied all my heights and widths by (3.0 / displayDensity) and it now looks the same across devices.

    The only caveat I found is that since it retains the exact size as the original, some portions of the screen might be truncated if you run it on a phone with smaller screen dimensions.

    Once again, my code might be extremely convoluted but it works on most devices I've tried it on for the time being (ranging from 320 dpi to 640 dpi). If anyone has any more elegant suggestions I'd love to hear them.

  • edited April 2018

    You can scale according width and height.

    I have 8 buttons that display the fulll width of the screen so I just set the button dimensions to width/8.

    I am sure there are good technical reasons for using pixel density etc.. but not sure what they are!

  • @hudson_m4000 Thanks for the comment! I was originally going to do that but my game is meant to be played in both portrait and landscape mode and when I tried that it messed up the proportions.

  • edited April 2018

    For that, you can still use width and height. In setup() use an if/else to set sizes dependent on whether in portrait or landscape mode ...

    Or maybe use min(width/height) when creating objects?

  • Please provide an MCVE demonstrating your problem.


  • edited April 2018 Answer ✓


    screens are classified by android according to pixels density; medium density is 160dpi: so, in order to adapt your app to the user screen you have to use (with AS and xml) dp (or dip) units which are "abstracted" units and mean "density independant pixels"; with P5 as you cannot use these units you have to "transform" your pixels in dip (or dp); that is easy, it's like "scaling".

    • First you get the screen density by code, writing float fact = getActivity().getResources().getDisplayMetrics().density;

    • it wil return some factor depending of the phone; if you are using a "medium 160dpi phone" it will return 1.0; if you are using a low density screen it will return 0.75; if you are using a xxhdpi phone it will return 2.0...and so on;

    • now scaling your buttons or textSize() or.... is easy: a button with 20px width appears 20px width on a 160dpi phone but it will appear as 10 on a Xhdpi phone: so you have to give it a size of 20*2.0. That s' all.

Sign In or Register to comment.