8

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?

Hadas Kaminsky
  • 1,285
  • 1
  • 16
  • 39
  • I'm not too familiar with the situation, but is there a way to get the nav bar (e.g. `getNavigationBar()`) and check if `isVisible()` or not? If so, can you get the height of the navbar and add it to the screen? Or is the navbar area "restricted" in some way regardless of visibility? – code_dredd Sep 13 '17 at 16:40
  • Have you tried to use the [Immersive Full-Screen Mode](https://developer.android.com/training/system-ui/immersive.html)? – Eselfar Sep 14 '17 at 11:21

2 Answers2

2

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

morH
  • 31
  • 8
1

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
Chris
  • 147
  • 1
  • 11