Search code examples
androidandroid-8.0-oreoandroid-8.1-oreoandroid-9.0-pie

How can we get the size of screen with notch on the top


There are some devices such as Huawei p20(running android 8,8.1) which has in-built notch on the top. The current way to get the size of the screen is

 Display display = getWindow().getWindowManager().getDefaultDisplay();
 Point screenSize = new Point();

 display.getRealSize(screenSize);
 //screenSize.x
//screenSize.y

And it's leading the game out of screen. So, is there any way to solve notch issue with android P and Pre-p devices.


Solution

  • There are only a few android devices with notch option and currently it is not possible to get the exact screen size of such devices using Android APIs.

    BUT there is a work around, you can check the name of device and adjust your layout accordingly.

    There is one popular Android library to get the market name of an Android device. Check more information here : https://github.com/jaredrummler/AndroidDeviceNames

    How to use this lib:

    String deviceName = DeviceName.getDeviceName();
    

    Hope this will help you to focus on your development instead of wasting time!