Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 954
Javasamurai Avatar asked May 23 '18 07:05

Javasamurai


2 Answers

The status bar(where all your notifications are) always adjusts to the depth of the notch. Look at the pixel 3 which has the largest notch in the world :p The status bar becomes as tall as the notch.

enter image description here

So the notch is only a problem if your app is in a full screen(content draws underneath the status bar) and it may cover some of your content. Make your app non-full screen (only draw below the status bar) and you don't have to worry about any notch size in the future. A lot of games ignore the status bar from their content.

like image 77
psydj1 Avatar answered Nov 12 '22 01:11

psydj1


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!

like image 43
Vikasdeep Singh Avatar answered Nov 12 '22 02:11

Vikasdeep Singh