Search code examples
androidandroid-layoutandroid-resourcesscreen-orientationscreen-size

How to differentiate a 16:9 display from a 18:9 one?


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.


Solution

  • 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