8

I am trying to figure out how I can tell the location of the status bar (top or bottom). I tried to inspect HierarchyViewer but didn't see the status bar view.

Really what I need to figure out is, given a context, a way to return a boolean (true if bar is on top, false if it isn't -- like it isn't on most tablets). I wrote a simple solution to try and figure out of the status bar is at the top or bottom, but it doesn't seem to be helping:

private boolean isStatusBarAtTop(){
    if (!(getContext() instanceof Activity)) {
        return !getContext().getResources().getBoolean(R.bool.isTablet);
    }

    Window window =  ((Activity) getContext()).getWindow();

    if(window == null) {
        return !getContext().getResources().getBoolean(R.bool.isTablet);
    }

    Activity activity = (Activity)getContext();
    Rect rect = new Rect();

    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    View ourView = window.findViewById(Window.ID_ANDROID_CONTENT);

    Log.d("Menu","Window Top: "+ ourView.getTop() + ", "+ourView.getBottom()+ ", "+ourView.getLeft()+", "+ourView.getRight());
    Log.d("Menu","Decor View Dimensions"+rect.flattenToString());

    return  ourView.getTop() != 0;
}

And for some reason I get the following as output (running on Nexus 7 Tablet):

D/Menu(1007): Window Top: 0, 0, 0, 0
D/Menu(1007): Decor View Dimensions0 0 800 1216

What am I thinking/doing wrong?

hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • How about simply not hacking into `DecorView`, since that may not be reliable? What are you trying to achieve? – CommonsWare Sep 11 '12 at 23:51
  • Create a SlidingMenu menu (that also slides out `ActionBar`). – hwrdprkns Sep 11 '12 at 23:56
  • [SimonVT](https://github.com/SimonVT/android-menudrawer/commit/49b9e32f1e94118a3c56677a8b0dadb1129909ef) has figured something out -- but is there another, more general solution? Preferably a standalone method? – hwrdprkns Sep 12 '12 at 20:19

3 Answers3

5

This is the only method that worked for me. The following method returns the height of the top statusbar. So if the according return value equals 0, there is no statusbar at the top of your window.

public static int getTopStatusBarHeight(Activity activity) {
    Rect rect = new Rect();

    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    return rect.top;
}
droide_91
  • 197
  • 2
  • 6
  • I voted @kalandar's answer before testing it throughly and now I can't remove my vote. His solution doesn't work in every case, but this one does (in all the cases I could test it with in my app, at least). This is the better/best answer. – nonzaprej Sep 12 '15 at 16:53
  • 3
    Note that a translucent status bar will cause this to return 0. – Simas Nov 10 '15 at 13:55
1

There really isn't a way to tell the orientation/position of the status bar. The best way to account for it is to make sure your view is always accounting for it.

@Override
protected boolean fitSystemWindows(Rect insets) {
    RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)this.getLayoutParams());
    int bottom = params.bottomMargin;
    int left = params.leftMargin;
    int right = params.rightMargin;
    params.setMargins(left, insets.top, right, bottom);
    return super.fitSystemWindows(insets);
}

The approach behind this method (which you override in your ViewGroup) is to receive an inset rect from the framework and then add those insets to your view padding.

hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
1

I just found the another way to find out the position of statusbar. the logic is simple, we have 2 steps to achieve this, first one is,

 int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();

runnig this code will give you a (titlebar+statusbar) height if the statusbar is located on top, otherwise this will return 0. so we can assume that the output is 0 the statusbar is located on bottom, otherwise the statusbar located on top.

now the statusbar is on bottom or top thats not an issue we can get the height like that,

public int getStatusBarHeight() 
{
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) 
    {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

i hope this will help someone....

kalandar
  • 793
  • 6
  • 13
  • this helped to point me in the right direction, except i found the exact *opposite* result for contentViewTop. If it's 0, that means the status bar is on the *bottom*. tested on Android 4.1.1 (Nexus 4 and Galaxy Tab), 4.2.2 (Galaxy S3), 4.3 (old Nexus 7), and 4.4.2 (new Nexus 7 and Nexus 5). – ryan May 02 '14 at 22:11
  • 1
    @ryan I think you've misread his answer, because he does state if it's 0 the status bar is at the bottom. (And no, he hasn't edited his post.) – Kevin Cruijssen Aug 07 '14 at 07:43