Hadas Kaminsky
Hadas Kaminsky

Reputation: 1311

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?

Upvotes: 8

Views: 1894

Answers (2)

Chris
Chris

Reputation: 147

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

Upvotes: 1

morH
morH

Reputation: 31

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

Upvotes: 2

Related Questions