4

My app is in sticky immersive mode that the system navigation bar (I call it SNB) is hidden in running time of my app. It works well but exists one problem, it is each time I show a DialogFragment, the SNB will be appear. How can I hide it.

I have written a few line of code in my acivity class to make my app become fullscreen and sticky immersive mode.

MyActivity.java

@Override
protected void onResume() {
    super.onResume();
    hideSystemUI();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI();
}

public void hideSystemUI() {
    // Enables regular immersive sticky mode.
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    );
}

MyDialog.java

public class MyDialog extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog alertD = builder.create();
        alertD.setView(view);
        return alertD;
    }

    public void show() {
        Activity activity = getActivity();
        show(activity.getSupportFragmentManager(), "dialog");
    }

    @Override
    public void onResume() {
        super.onResume();
        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);
    }
}

I followed the answer to show dialog without show SNB: https://stackoverflow.com/a/33589793/10467639 but it does not work as desired

MyDialog dialog = new MyDialog();
// Set the dialog to not focusable.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.show();
// Set the dialog to focusable again.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

style.xml

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/themeColor</item>
        <item name="colorPrimaryDark">@color/themeColor</item>
        <item name="colorAccent">@color/themeColor</item>
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/black</item>
        <item name="colorControlHighlight">@color/white</item>
    </style>
    <style name="BoldGrayHeaderTextView">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_bold</item>
        <item name="android:textColor">@color/darkColor</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <!-- For CarPinItem on Map -->
    <style name="carPinItemUnSelected">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/generalTextColor</item>
    </style>

    <style name="carPinItemSelected">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/white</item>
    </style>

    <!-- Style for drawer navigation item                                                                       -->
    <style name="NavigationDrawerStyle">
        <item name="android:textSize">15dp</item><!-- text size in menu-->
        <item name="android:listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
        <item name="listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
    </style>

    <!--Style for textInputEditText   -->
    <style name="TextInputLayoutStyle">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textSize">13dp</item>
        <item name="android:textColor">@color/white</item>

    </style>
    <!--Style for profile bottom menu   -->
    <style name="BottomTextLayoutStyle">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/black</item>
    </style>

    <style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
        <item name="android:textColorPrimaryInverse">@color/black</item>
        <item name="android:colorControlActivated">@color/successColor</item>
    </style>

    <style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
        <item name="colorControlHighlight">@color/white</item>

    </style>

    <!--BottomNavivagionView textsize-->
    <style name="Widget.BottomNavigationView"
        parent="Widget.Design.BottomNavigationView">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
    </style>

    <style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
    </style>
</resources>

How can I solve this problem? Thank in advanced!

  • Hey can you also show your `styles.xml` file here. – ravi Jul 11 '19 at 09:36
  • @ravi I think the problem not cause by XML, the XML file actually is very long and confusing! It's just described my activity UI! –  Jul 11 '19 at 09:45
  • we should not really make assumptions about the cause of a problem. But if you think this much info gets your problem solved, well good luck. – ravi Jul 11 '19 at 10:00
  • @ravi I am sorry for my naive assumtion. I have edited my answer with `style.xml` file. Can you help me again? –  Jul 12 '19 at 00:53
  • It's alight man. I have posted an answer, try that and let me know if that helps – ravi Jul 15 '19 at 08:48

2 Answers2

8

Followed this answer https://stackoverflow.com/a/23207365/8531215 ,I have tested and it works fine! modify onCreateDialog() method a little bit

public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog alertD = builder.create();
        alertD.setView(view);
        Dialog dialog = alertD.create();
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        //Show the dialog!
        dialog.show();

         //Set the dialog to immersive sticky mode
        dialog.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        //Clear the not focusable flag from the window
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        return alertD;
}
TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • It worked, but in my scenario naviagation bar appear for less than a sec. Is there any solution for navigation bar to not to appear. I have a dialogfragment in player for video quality selection. – Rohaitas Tanoli Dec 03 '20 at 13:16
  • This is work solution which no need to touch style.xml and using IMMERSIVE_STICKY flag. With no STICKY there may be different scenario. – Yeung Jul 20 '21 at 08:21
0

According to the docs here:

Implement onWindowFocusChanged(). If you gain window focus, you may want to re-hide the system bars. If you lose window focus, for example due to a dialog or pop up menu showing above your app, you'll probably want to cancel any pending "hide" operations you previously scheduled with Handler.postDelayed() or something similar.

So What I suggest you do is, change your code in MyActivity.java from:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI();
}

to:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
     if (!hasFocus){
            //we lost focus due to DialogFragment
            hideSystemUI();            
        }
}
ravi
  • 899
  • 8
  • 31