2

In my Android App i use different Android Resource Directories to support multiple screen sizes and orientations. So inside res i have a layout folder, a layout-land folder, a layout-large folder, a layout-large-land folder and so on.

This approach worked perfectly for me until i tried installing my app in a newer phone that has a 18:9 display instead of a 16:9 one.

This phone draws the resources from the layout and layout-land folders, meaning that it is getting registered as a "normal" screen.

Moreover, if i try getting the screen size of this phone using the following piece of code,

int screenSize = getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK;


        switch(screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                Toast.makeText(this, "Extra Large Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                Toast.makeText(this, "Large Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                Toast.makeText(this, "Normal Screen", Toast.LENGTH_LONG).show();
                break;
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                Toast.makeText(this, "Small Screen", Toast.LENGTH_LONG).show();
                break;
            default:
                Toast.makeText(this, "Screen size is neither xlarge, large, normal or small", Toast.LENGTH_LONG).show();
        }

then it once again gets registered as a normal screen. But so does the phone with the 16:9 display. Needless to say this creates small but noticeable differences in how the app looks on those two phones.

So my question is, is there a way to differentiate those two displays?

Note: I am aware that there are workarounds around this, like avoiding dp values altogether and just using weights but what i'd like to know is if there is an "official" or "advised" method to treat this case. Much like the layout-land folder for landscape orientation or layout-xlarge folder for extra large screen and so on.

Thanks in advance.

Sheharyar
  • 73,588
  • 21
  • 168
  • 215

1 Answers1

5

Device fragmentation has always been a huge problem for Android. Sadly, the same thing that makes Android so powerful is also a huge source of pain. You can't really predict how the device manufacturers will change different aspects of the device including the screen and the aspect ratio.

But you can check the ratio yourself and make changes according to that (but I guess this is not "official" way you had in mind):

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float ratio = ((float)metrics.heightPixels / (float)metrics.widthPixels);

Source

Sheharyar
  • 73,588
  • 21
  • 168
  • 215
  • So, course of action is to use the same layout for both devices and then see what needs to be changed in the 18:9 device ( some view needs to be a bit to the left, a button needs to be slightly smaller e.t.c) and do that programmatically for this device using layourparams and so on. Right? – Sotiris S. Magionas Mar 08 '18 at 12:54
  • Yes. Although, it might be simpler if you used a `ConstraintLayout`. – Sheharyar Mar 08 '18 at 12:55
  • Ok, thanks. I will upvote and accept your answer. Thanks for your time and effort. – Sotiris S. Magionas Mar 08 '18 at 12:56
  • Does not give exact screen height. Always returns a bit less – Jemshit May 28 '18 at 13:38
  • 1
    It does not return the screen size but the window size witch is the portion of the screen taken by your app. – Mihai Jul 12 '18 at 10:12