Search code examples
androidandroid-fragmentsandroid-notification-barandroid-popupwindow

PopupWindow + full screen => show notification-bar?


Background

I'm making an activity that has 2 fragments.

the second fragment shows a full screen image, which also hides the status bar (AKA notification bar) using:

    final View decorView = getActivity().getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
    if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
        uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

The problem

I'm trying to add a sharing dialog of the currently shown image, but no matter which method I try to do it, it always shows the status bar, making things "jumpy".

What I've tried

The normal way to open the sharing dialog didn't work (I mean, it worked, but it showed the status bar) :

startActivity(Intent.createChooser(shareIntent,"some text");

So I had to create my own popup.

First I wanted to show some sort of progressDialog, but it also caused the status bar to show, so I removed it.

Then I wanted to show a list of the items, so I used ListPopupWindow (anchored to the view the user is clicking) , but it also showed the status bar. Another thing I tried is to use the normal PopupWindow and set a listView inside of it, but it also didn't help.

Another thing that I tried is to change the window of the popupWindow to have the same settings as the fragment's activity, but it also didn't help.

I even tried to set a smaller height for the popup, but it didn't help.

The question

How can I use a PopupWindow (or the normal sharing dialog) so that the status bar will remain in the same state as it did before showing it?


Solution

  • OK, I think I've found the perfect combination. I used the normal sharing intent, and set the flags of the window as needed.

    for hiding/showing the status bar correctly :

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setStatuBarVisibility(final boolean visible) {
        if (visible) {
            // if (Build.VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN)
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            final View decorView = getActivity().getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            // a no animation version of: getActivity().getActionBar().show();
            ViewUtil.getActionBarView(getActivity()).setVisibility(View.VISIBLE);
        } else {
            // hide status bar (if possible) and the action bar, and dim navigation buttons:
            final View decorView = getActivity().getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
            if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN)
                uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            // if (Build.VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN)
            getActivity().getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            // a no animation version of: getActivity().getActionBar().hide();
            ViewUtil.getActionBarView(getActivity()).setVisibility(View.GONE);
            ViewUtil.hideSoftKeyboardFromFocusedView(getActivity());
        }
    }
    
    public static View getActionBarView(final FragmentActivity activity) {
        final Window window = activity.getWindow();
        final View v = window.getDecorView();
        final int resId = activity.getResources().getIdentifier("action_bar_container", "id", "android");
        return v.findViewById(resId);
    }
    
    public static void hideSoftKeyboardFromFocusedView(final Context context, final View view) {
        final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    

    it also avoids the jumpy behavior when going to/from the fragment, and when showing the sharing dialog.