Search code examples
xamarinxamarin.formsxamarin.androidon-screen-keyboard

Hidden navigation bar in xamarin forms shows up when on screen keyboard comes up on Android


For a project the bottom navigation bar in an android app needs to be not visible. Found this code on browsing around and it works initially: This is some code in MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    HideNavAndStatusBar();
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

private void HideNavAndStatusBar()
{
    int uiOptions = (int)Window.DecorView.SystemUiVisibility;
    uiOptions |= (int)SystemUiFlags.LowProfile;
    uiOptions |= (int)SystemUiFlags.Fullscreen;
    uiOptions |= (int)SystemUiFlags.HideNavigation;
    uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
    Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
}

This code works until the on screen keyboard comes up when tapping on an entry. This shows the navigation bar again and it stays on even when the keyboard is closed. Similarly when a picker is tapped, the navigation bar pops up as well, when the picker window is closed the navigation bar is gone again.

This code will be used for an internal system and it is very important that the user can not go and mess around on the android os.

Any ideas on how to fix this?


Solution

  • This code works until the on screen keyboard comes up when tapping on an entry. This shows the navigation bar again and it stays on even when the keyboard is closed.

    When you click the Entry or Picker, this navigation bar will appear, it cannot be changed. But if keybord or picker is disappear, make the navigation bar is hide, it could be achieved like this GIF.

    enter image description here

    You can call HideNavAndStatusBar method in SystemUiVisibilityChange event again like following code.

      [Activity(Label = "App31", Icon = "@mipmap/icon", Theme = "@style/MainTheme",WindowSoftInputMode =SoftInput.AdjustPan, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;
                HideNavAndStatusBar();
    
                Window.DecorView.SystemUiVisibilityChange += DecorView_SystemUiVisibilityChange;
                base.OnCreate(savedInstanceState);
    
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);          
                LoadApplication(new App());
            }
    
            private void DecorView_SystemUiVisibilityChange(object sender, View.SystemUiVisibilityChangeEventArgs e)
            {
    
                HideNavAndStatusBar();
            }
    
    
            private void HideNavAndStatusBar()
            {
                int uiOptions = (int)Window.DecorView.SystemUiVisibility;
                uiOptions |= (int)SystemUiFlags.LowProfile;
                uiOptions |= (int)SystemUiFlags.Fullscreen;
                uiOptions |= (int)SystemUiFlags.HideNavigation;
                uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
                uiOptions |= (int)SystemUiFlags.LayoutStable;
    
                Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
            }
    }