Laetan
Laetan

Reputation: 899

How to disable Android status bar interaction

Is there a way on android to keep the status bar while disabling all interaction you can do with it, like pulling it down?

I want to keep the information this bar gives, but I don't want users to interact with it.

Upvotes: 1

Views: 979

Answers (1)

Matter Cat
Matter Cat

Reputation: 1578

This is the method that I like to use. You can unwrap it from the method and place it inside a base Activity instead. iirc, I got this from StackOverflow as well, but I didn't make a note of it so I'm not sure where the original post is.

What it basically does is place a transparent overlay over the top bar that intercepts all touch events. It's worked fine for me thus far, see how it works for you.

You MAY need to put this line in the AndroidManifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

I have it in my project, but I can't remember if it's because of this or something else. If you get a permission error, add that in.

WindowManager manager;
CustomViewGroup lockView;

public void lock(Activity activity) {

    //lock top notification bar
    manager = ((WindowManager) activity.getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE));

    WindowManager.LayoutParams topBlockParams = new WindowManager.LayoutParams();
    topBlockParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    topBlockParams.gravity = Gravity.TOP;
    topBlockParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
            // this is to enable the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
    topBlockParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    topBlockParams.height = (int) (50 * activity.getResources()
            .getDisplayMetrics().scaledDensity);
    topBlockParams.format = PixelFormat.TRANSPARENT;

    lockView = new CustomViewGroup(activity);
    manager.addView(lockView, topBlockParams);
}

and CustomViewGroup is

private class CustomViewGroup extends ViewGroup {
    Context context;

    public CustomViewGroup(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.i("StatusBarBlocker", "intercepted by "+ this.toString());
        return true;
    }
}

Also! You also have to remove this view when your activity ends, because I think it will continue to block the screen even after you kill the application. Always, always ALWAYS call this onPause and onDestroy.

    if (lockView!=null) {
        if (lockView.isShown()) {
            //unlock top
            manager.removeView(lockView);
        }
    }

Upvotes: 1

Related Questions