Danny
Danny

Reputation: 3690

How do I position my UI below the Android status bar?

I've found many questions around showing UI on "top" of (covering) and under (obscured by) the status bar, depth-wise. But if you have a notification-like activity that comes down from the top, how do you position it so it automatically comes down past the status bar?

Otherwise, the status bar overlaps with and obscures the UI that just came down.A screenshot of a UI element coming down from the very top of the app but obscured by the status bar.

Do I simply need to make the UI taller by \status-bar height\ to handle this overlap?

Upvotes: 3

Views: 609

Answers (2)

Paulo Avelar
Paulo Avelar

Reputation: 2140

You have to programatically add padding to your activity in order to prevent that. I came up with a method to do that, by gathering information from many sources, even here in Stack Overflow.

Try to adapt this to your needs:

private void fixActivityPadding() {
    // gets additional padding (final padding: status bar height + custom padding)
    int padX = getResources().getDimensionPixelSize(R.dimen.welcome_padding_x);
    int padY = getResources().getDimensionPixelSize(R.dimen.welcome_padding_y);
    // gets navigation and status bar heights
    int navBarHeight = getResourceHeight("navigation_bar_height");
    int statusBarHeight = getResourceHeight("status_bar_height");

    // gets the layout in the Activity
    View layout = findViewById(R.id.activity_layout);
    // sets padding accordingly, considering orientation (nav bar position)
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        layout.setPadding(padX, statusBarHeight + padY, padX, navBarHeight + padY);
    } else {
        layout.setPadding(padX, statusBarHeight + padY, navBarHeight + padX, padY);
    }
}

// helper method
private int getResourceHeight(String identifier) {
    int result = 0;
    int resourceId = getResources().getIdentifier(identifier, "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

I'm just not sure what you mean by Notification Activity, but I believe this should help you.

Upvotes: 1

Robin Dijkhof
Robin Dijkhof

Reputation: 19288

I think this is just the WindowManager.LayoutParams.FLAG_FULLSCREEN. Addt his to your activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

The status bar becomes visible again when the user swipes from the top.

Upvotes: 0

Related Questions