Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the correct screen display size on Galaxy S8

In my app I am currently using windowManager.getDefaultDisplay() to determine the screen size.

With the new Samsung S8 navigation bar, that can be shown and hidden using a 'dot' on the bottom left, I get the same display size for navigation bar shown or hidden. It always returns the display size as if the navigation bar is shown - I expect to get 1080x2220 when the navigation bar is hidden, but always gets 1080x2076.

Is there any way I can distinguish the cases programmatically and obtain the correct screen dimensions so I can display my app correctly?

like image 418
Hadas Kaminsky Avatar asked Sep 13 '17 16:09

Hadas Kaminsky


2 Answers

I had the same problem as you, and the solution I found was to measure the height of the content root view :

findViewById(android.R.id.content).getHeight()

hope it will help you.

check here for more detailes about android.R.id.content

like image 110
morH Avatar answered Sep 23 '22 02:09

morH


I also had the same problem but the solution with the content root view didn't work for me (the height i got was 2148).

But i could solve this with the decorview:

    //Get the correct screen size even if the device has a hideable navigation bar (e.g. the Samsung Galaxy S8)
    View decorView = getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
    Rect r = new Rect();
    decorView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom; // =2220 on S8 with hidden NavBar and =2076 with enabled NavBar
int screenWidth = r.right; // =1080 on S8
like image 30
Chris Avatar answered Sep 24 '22 02:09

Chris