2

I am writing an android application which should run in the immersive mode. I want to hide the navigation bar and for that I wrote a method as :-

public void hideSystemUI()
{

       getWindow().getDecorView().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 // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
    );
}

I also wrote a method to detect navigation bar's visibility and then hide it if visible.

private void systemUIListener()
{
    //listens and check if app is in immersive mode or not
    View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    // Note that system bars will only be "visible" if none of the
                    // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                        // TODO: The system bars are visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls
                        hideSystemUI();
                    }
                }
            });
}

I am calling these 2 methods one after another inside my onCreate() method :-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    hideSystemUI();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    setContentView(R.layout.activity_main);
   // hide system ui
   systemUIListener();
}

My problem is that, the app works perfectly for sometime, that is, the navigation bar comes and immediately hides but after a few swipes, the navigation bar wont hide. It will be visible through out.

I have also noticed that, the “speak-now google” is also launched when the home button is long pressed.

I am working on android 5.0.1 version and my device is lenovo tab 2.

My question :-

How to hide my navigation bar each time a swipe is made ? And also how to prevent the “speak-now” from launching when home is long pressed ?

0 Answers0