Android Hide Navigation Bar/Stay in Immersive Mode with Soft Keyboard Appearance

后端 未结 6 1374
时光说笑
时光说笑 2020-12-09 03:34

Working on a client\'s app that is using immersive mode to hide the navigation bar and status bar on every activity using the following code:

int currentApiV         


        
相关标签:
6条回答
  • 2020-12-09 03:43

    See this. I've searched for over 3 hours for this problem and that solution worked well. I hope it'll be helpful.

    0 讨论(0)
  • 2020-12-09 03:45

    To type in soft input without loosing immersive or immersive sticky mode Check this

    0 讨论(0)
  • 2020-12-09 03:49

    I think that is not possible. Here

    Flutter: How to show onscreen keyboard without android's bottom navigation bar on focusing a textfield?

    is stated clearly and my experience seems to confirm it.

    the following code placed in an activity

    private var appVisibility:Int = (  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                    or View.SYSTEM_UI_FLAG_LOW_PROFILE
                                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        window.decorView.systemUiVisibility = appVisibility
    
    }
    override fun onResume() {
        super.onResume()
        updateUI()
    }
    fun updateUI() {
        val decorView = window.decorView
        decorView.setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
                decorView.systemUiVisibility = appVisibility
            }
        }
    }
    

    correctly hides the nav bar always (when you swipe from the bottom of the screen nav bar appears for a couple of seconds and then disappears) but when keyboard is on. I also tried to open keyboard programmatically and then hide the nav bar after few seconds, without any success.

    private fun toggleIME(){
        val imm             = applicationContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
        val view: EditText? = text
    
        if(view != null && imm != null) {
            isKOpen = !isKOpen
            if (isKOpen) {
                view.requestFocus()
                imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)
    
                handler.postDelayed(Runnable { // execute after XXXms
                    updateUI()
                    window.decorView.systemUiVisibility = appVisibility
                }, 5000)
    
            }
            else {
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }
        }
    }
    

    I hope to be missing something. I would need that for a custom keyboard I'm developing (where I have to implement the back button)

    0 讨论(0)
  • 2020-12-09 03:50

    Here is my solution for this ; First I checked if soft keyboard is showed up or not:

    getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
    
                    Rect r = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                    int screenHeight = getWindow().getDecorView().getRootView().getHeight();
    
                    int keypadHeight = screenHeight - r.bottom;
    
                    //Log.d(TAG, "keypadHeight = " + keypadHeight);
    
                    if (keypadHeight > screenHeight * 0.15) { 
                         //Keyboard is opened
                         hideNavBar();
                    }
                    else {
                        // keyboard is closed
                    }
                }
            });
    

    And I have a hideNavBar() method to be triggered when soft keyboard is showed up.

    private void hideNavBar() {
        if (Build.VERSION.SDK_INT >= 19) {
            View v = getWindow().getDecorView();
            v.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);
        }
    }
    

    This solves the problem of getting navigation bar while there is an Edittext to be typed.

    0 讨论(0)
  • 2020-12-09 04:01

    Updated:

    I have a painting app (Paint Shapes) and this is the configuration I used to always be inside the immersive mode. I use the onWindowFocusChanged method.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            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
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 04:02

    I've come up with a workaround that checks the navigation bar's status for every internal, try to hide it and check it again (and again).

    Here's some piece of code, that makes sure the navigation bar is hidden in 2 seconds after the soft keyboard is closed.

      private final Runnable checkSystemUiRunnable = new Runnable() {
        @Override
        public void run() {
          checkHideSystemUI();
        }
      };
    
      private void checkHideSystemUI() {
        // Check if system UI is shown and hide it by post a delayed handler
        if (isSystemUiShown) {
          hideSystemUI();
          handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
        }
      }
    
      private void hideSystemUI() {
        decorView.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);
      }
    
      // In onCreate()
        decorView.setOnSystemUiVisibilityChangeListener(
                new View.OnSystemUiVisibilityChangeListener() {
                  @Override
                  public void onSystemUiVisibilityChange(int visibility) {
                    if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                      handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
                      isSystemUiShown = true;
                    } else {
                      isSystemUiShown = false;
                    }
                  }
                });
    
    0 讨论(0)
提交回复
热议问题