0
votes

I need to hide navigation bar in Xamarin Forms application permanently. Application is destinated to be installed on Android device. I mean, device with will be open to public (like restaurant's kiosk). I want to start my application only and prevent user from going to the system.

I read this: https://developer.android.com/training/system-ui/navigation

I tried this immersive sticky mode. At start status bar and navigation bar are hidden. When I swipe at top or at bottom, the navigation bar appears. How to prevent or disable it?

I read about rooting device and uninstall UISystem.apk and boot indicated app but I think the warranty for device will be lost. Is there another way?

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

            base.OnCreate(savedInstanceState);
    HideSoftwareMenuBars();
                LoadApplication(new App());
    }

    public override void OnWindowFocusChanged(bool hasFocus)
            {
                base.OnWindowFocusChanged(hasFocus);
                if (hasFocus)
                    HideSoftwareMenuBars();
            }

    protected override void OnResume()
            {
                base.OnResume();
                HideSoftwareMenuBars();
            }


     private void HideSoftwareMenuBars()
            {
                int uiOptions = (int)Window.DecorView.SystemUiVisibility;

                uiOptions |= (int)SystemUiFlags.LayoutStable;
                uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
                uiOptions |= (int)SystemUiFlags.LayoutFullscreen;

                uiOptions |= (int)SystemUiFlags.LowProfile;
                uiOptions |= (int)SystemUiFlags.HideNavigation;
                uiOptions |= (int)SystemUiFlags.Fullscreen;

                uiOptions |= (int)SystemUiFlags.ImmersiveSticky;

                this.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
            }
1
You might want to use a Kiosk mode: developer.android.com/work/dpc/dedicated-devices Xamarin Sample (by 3rd party): github.com/Cheesebaron/KioskModeSamplejgoldberger - MSFT
I have run that github solutions. The author implement function that if user press home button or "see working application" button, it starts again the kiosk application. It works slowly, so when user goes to see all applications, swipe the kiosk application (close it) then he can do whatever he want in operation system...YoungEddie

1 Answers

0
votes

You can implement it by using MessagingCenter

in ContentPage

For example , It will hide or show the status bar when click a button in the page . You can invoke it when the page scroll to top or bottom .

bool isHaveBar = false;
public MainPage()
{
   InitializeComponent();
   NavigationPage.SetHasNavigationBar(this, false);          
}

private void Button_Clicked(object sender, EventArgs e)
{
   isHaveBar = !isHaveBar;
   NavigationPage.SetHasNavigationBar(this, isHaveBar);
   MessagingCenter.Send<Object,bool>(this,"Hide",isHaveBar);
}

Don't forget to set the MainPage of App as NavigationPage

MainPage = new NavigationPage(new YourContentPage());

in Android MainActivity

protected override void OnCreate(Bundle savedInstanceState)
{
   //...
   base.OnCreate(savedInstanceState);
   MessagingCenter.Subscribe<Object, bool>(this, "Hide", (arg,isHaveBar) => { 
            
   if(isHaveBar)
   {
      Window.ClearFlags(WindowManagerFlags.Fullscreen);
      Window.AddFlags(WindowManagerFlags.ForceNotFullscreen);
   }
                
   else
   {
      Window.AddFlags(WindowManagerFlags.Fullscreen);
      Window.ClearFlags(WindowManagerFlags.ForceNotFullscreen);
   }
            
 });

  //...
}