Hide navigation bar while popup menu is showing

2.2k views Asked by At

My application works in full screen mode. Below code to hide navigation bar.

   public static void hideDefaultControls(@NonNull final Activity activity) {
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        final Window window = activity.getWindow();

        if (window == null) {
            return;
        }

        final View decorView = window.getDecorView();

        if (decorView != null) {
            int uiOptions = decorView.getSystemUiVisibility();

            if (Build.VERSION.SDK_INT >= 14) {
                uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
            }

            if (Build.VERSION.SDK_INT >= 16) {
                uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            }

            if (Build.VERSION.SDK_INT >= 19) {
                uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            }

            decorView.setSystemUiVisibility(uiOptions);
        }
    }

android.widget.PopupMenu;

PopupMenu popupMenu = new PopupMenu(context, anchor);

//init code...
popupMenu.show();

I want have full screen mode while popup menu is showing. I'm trying to call hideDefaultControls() method after popup shown, but controls is hiding only after popup hidden.

1

There are 1 answers

0
Hong Nguyen On

Because PopupMenu use new DecorView (PopupDecorView), try:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
    fullSceen(getWindow().getDecorView());
}
else {
    try {
        Object wmgInstance = Class.forName("android.view.WindowManagerGlobal").getMethod("getInstance").invoke(null);
        Field viewsField = Class.forName("android.view.WindowManagerGlobal").getDeclaredField("mViews");

        viewsField.setAccessible(true);
        ArrayList<View> views = (ArrayList<View>) viewsField.get(wmgInstance);
        for (View view: views) {
            fullSceen(view);

            view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    fullSceen(view);
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    AppUtils.console(this,TAG, "hasFocus == false");
}
}
private void fullSceen(View view){
view.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}