Getting the actual screen height (android)

前端 未结 4 1078
北海茫月
北海茫月 2021-01-17 10:36

I want to get the actual screen height of the device that runs my app. To achieve this i try the following:

DisplayMetrics metrics = new DisplayMetrics();
get         


        
4条回答
  •  孤独总比滥情好
    2021-01-17 11:10

    I needed to get the actual available height, and out of all the options this is the only thing that worked for me.

    Create a rectangle, and get its dimensions:

    Rect r = new Rect();
    activityRootView.getWindowVisibleDisplayFrame(r);
    int screenHeight = r.bottom - r.top;
    

    where activityRootView is the root view of your layout.

    If you need to extract the toolbar height:

    int availableScreenHeight = screenHeight - toolbar.getHeight();
    

    where toolbar is your toolbar view.

    availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).

提交回复
热议问题