3

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?

Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32
Gert Hermans
  • 769
  • 1
  • 9
  • 30

2 Answers2

2

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;
        }
}
Leon
  • 8,404
  • 2
  • 9
  • 52
  • Thanks, this improves it a bit, still the navigation bar appearing is still a blocking issue. – Gert Hermans Jun 16 '20 at 11:47
  • It is by android design(You can see the Annotation about SYSTEM_UI_FLAG_FULLSCREEN和SYSTEM_UI_FLAG_HIDE_NAVIGATION):There is a limitation: because navigation controls are so important, the least user      * interaction will cause them to reappear immediately.  When this happens, both      * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,      * so that both elements reappear at the same time. – Leon Jun 16 '20 at 12:05
-2

In case you have full control of the device it will run on. Like it was the case in my problem, then this is a better solution as it does not let the navigation pop up anywhere (which could be problematic, but in some circumstances it might be exactly what you need) If you have a rooted device you can use the Android debug bridge (adb.exe) to always remove the navigation bar. This will disallow the user to go to the os.

adb shell pm disable com.android.systemui

I found this on this stack overflow link (the response of NartusTeam) Is there a way to hide the system/navigation bar in Android ICS

Gert Hermans
  • 769
  • 1
  • 9
  • 30
  • If you application running in Android device,will all users root their devices? – Leon Jun 18 '20 at 13:21
  • it is for a dedicated machine, not for the play store or anything – Gert Hermans Jun 18 '20 at 15:12
  • I see that it is not for most applications, but in our case it is a solution that works. The application runs on an device we control – Gert Hermans Jun 18 '20 at 15:14
  • This answer is just used for your environment, Not suitable for most people's use environment. And in your questions and answers, you also did not make it clear that your use environment will mislead others. I think my answer could be suitable for most of developers. – Leon Jun 18 '20 at 15:19
  • Alright I give the answer tag back to you and added a note to my answer (which actually did solve my problem better). – Gert Hermans Jun 18 '20 at 15:31