androidstatusbarandroid-notification-bar

How to lock status bar and notification bar in a specific Activity in my application


I am working on a exam related android application. When exam starts i need to lock status bar and notification bar. I used below code but it is only hiding status bar and notification bar.

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Is any way to lock status bar and notification bar instead of hiding them. For any positive response thanks in advance.


Solution

  • what do you mean locking the notification bar? you mean not receiving any notification for the moment?

    regarding your question. What you can do is make your Toolbar not clickable or more specifically make the Navigation Drawer not clickable so the user can not open it.

    EDIT:

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

    you can do this:

    WindowManager manager = ((WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE));
    
    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
    localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    localLayoutParams.gravity = Gravity.TOP;
    localLayoutParams.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;
    
        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;
    
        customViewGroup view = new customViewGroup(this);
    
        manager.addView(view, localLayoutParams);
    

    or look at this posted question: Disable the notification panel from being pulled down