Top-down status bar lock

I have a full screen application covering the whole screen including the top status bar . Since the top / bottom sub is allowed to display some parameters to the user, it happens that scrolling from top to bottom, the status bar is displayed (for example, when you want to see notifications and scroll from top to bottom), is there a way to avoid this?

+3


source to share


2 answers


Use the TYPE_SYSTEM_ERROR type for WindowManager.LayoutParams to create a hidden impossible full screen view. The buffers for displaying the status bar and navigation will be locked.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mMainLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_fullscreen, null);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
        PixelFormat.TRANSLUCENT);
    handleParams.gravity = Gravity.TOP;

    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(mMainLayout, handleParams);
}

      



And you should show it after turning on the display (unlock the screen) if necessary. Add code:

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

      

+1


source


There are several options. Before the line

super.onCreate (savedInstanceState)

add the following:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

      

This should get rid of the status bar completely. Basically, the unnamed function removes the top toolbar, and to ensure full screen size, we set full screen flags in height and width.

As far as preventing scrolling in general, there is a trick that can be used. Essentially an invisible top view once you're in full full screen mode that prevents any scrolling. For example,

View disableStatusBarView = new View(context);

WindowManager.LayoutParams handleParams = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.FILL_PARENT,
    <height of the status bar>,
    // This allows the view to be displayed over the status bar
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
    // this is to keep button presses going to the background window
    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,
    PixelFormat.TRANSLUCENT);

handleParams.gravity = Gravity.TOP;
context.getWindow().addView(disableStatusBarView, handleParams);

      



This creates an invisible view of the status bar, which will receive touch events and blocks events from accessing the status bar, therefore preventing it from expanding.

Alternatively, and this works best, override the windowFocusChanged method. Essentially you are NOT preventing the status bar from expanding, but YOU DO NOT PROVIDE to use. Because once it has expanded, it is closed. I say this works best because with invisible presentation methodology you can consider screen dimensions to really make it effective across the entire device. Thus, there is more work to be done.

First, declare the permission in the manifest

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

      

Then override the onWindowFocusChanged method,

public void onWindowFocusChanged(boolean hasFocus)
{
        try
        {
           if(!hasFocus)
           {
                Object service  = getSystemService("statusbar");
                Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");
                Method collapse = statusbarManager.getMethod("collapse");
                collapse .setAccessible(true);
                collapse .invoke(service);
           }
        }
        catch(Exception ex)
        {
        }
}

      

0


source







All Articles