hwrdprkns
hwrdprkns

Reputation: 7585

Figure out whether status bar is at top or bottom?

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?

Upvotes: 8

Views: 3546

Answers (3)

droide_91
droide_91

Reputation: 197

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;
}

Upvotes: 5

kalandar
kalandar

Reputation: 823

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....

Upvotes: 1

hwrdprkns
hwrdprkns

Reputation: 7585

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.

Upvotes: 1

Related Questions