Xamarin form (android) hide Navigation/status always

KEVIN KE 41 Reputation points
2022-08-09T09:25:26.1+00:00

I am facing a problem on Xamarin Form (android). I need the buttom navigation (back, circle, squre buttons) hide always. Now I can arrchive by set SystemUiFlags.ImmersiveSticky && SystemUiFlags.HideNavigation etc as below.

229369-screenshot-2022-08-09-172003.png

But the buttom navigation will show if I scroll up from bottom and it will hide in few seconds again.

Do you have any idea that I can make it never comes out as long as my APP is running? (btw, root is not possible)

Thanks & Regards
Kexin

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,311 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,471 Reputation points Microsoft Vendor
    2022-08-10T06:14:11.173+00:00

    Hello,​

    You can control the display priority to hide the bottom navigation completely. The display order can be viewed in the return value of the getWindowLayerFromTypeLw function in WindowManagerPolicy
    You can use the following code to hide the bottom navigation completely, I tested in android 10(API30), it is working. Please notice blow code is deprecated in android 11 or later.

       protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   int uiOptions = (int)Window.DecorView.SystemUiVisibility;  
                   uiOptions |= (int)SystemUiFlags.Fullscreen;  
                   uiOptions |= (int)SystemUiFlags.HideNavigation;  
                   uiOptions |= (int)SystemUiFlags.ImmersiveSticky;  
                   uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;  
                   uiOptions |= (int)SystemUiFlags.LayoutFullscreen;  
                   uiOptions |= (int)SystemUiFlags.LayoutStable;  
         
                   WindowManagerLayoutParams lp = Window.Attributes;  
                   lp.X = 0;  
                   lp.Y = 0;  
                   lp.Flags = WindowManagerFlags.Fullscreen;  
                   lp.SystemUiVisibility =(StatusBarVisibility) uiOptions;  
                    
                   base.OnCreate(savedInstanceState);  
         
       }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Graham McKechnie 321 Reputation points
    2022-08-30T23:53:55.04+00:00

    Leon Lu

    Why do you keep advising users here to use deprecated methods? SystemUiVisibility was deprecated way back in Android 10 - that was 2019. How about advising users to use replacements such as WindowInsetsController or better yet WindowInsetsControllerCompat. It is bad enough that VS2022 17.4 Prev 1.0 (the latest version as I write this) is still using starter templates that haven't been revised since 2019, but to give this type of advice to new users is ridiculous when you consider we are now releasing apps for Android 13. Why not at least mention that although it works as you say in Android 11, that you'll get warnings?


  2. Graham McKechnie 321 Reputation points
    2022-09-03T00:55:50.677+00:00

    @KEVIN KE

    All you need to know about WindowInsetsControllerCompat https://developer.android.com/develop/ui/views/layout/immersive

    The following is an example of an ImmersiveFragment class, so if you need an immersive fragment just inherit from this class.

    public class ImmersiveFragment : AndroidX.Fragment.App.Fragment   
        {  
              
            private WindowInsetsControllerCompat windowInsetsControllerCompat;  
      
            public override void OnStart()  
            {  
                windowInsetsControllerCompat = new WindowInsetsControllerCompat(Activity.Window, Activity.Window.DecorView);  
                base.OnStart();  
            }  
      
            #region OnResume  
            public override void OnResume()  
            {  
                base.OnResume();  
                HideSystemUi();  
            }  
            #endregion  
      
            #region OnPause          
            public override void OnPause()  
            {  
                base.OnPause();  
                ShowSystemUi();  
            }  
            #endregion  
      
            #region HideSystemUi  
            public void HideSystemUi()  
            {  
      
                // 17/01/2022 Added this reference as explanation of why we needed this code from Android 11 and on...  
                // Don't use android:fitsSystemWindows="true" anywhere.  
                // Refer to https://stackoverflow.com/questions/57293449/go-edge-to-edge-on-android-correctly-with-windowinsets/70714398#70714398 goto the bottom for this solution  
                if (Activity is MainActivity mainActivity)  
                {  
                    mainActivity.SupportActionBar.Hide();  
                    mainActivity.DisableDrawerLayout();      // Disable the navigationDrawer of the MainActivity. We don't want a user to be able to swipe it into view while viewing any of the gauges  
                }  
      
                WindowCompat.SetDecorFitsSystemWindows(Activity.Window, false);  
                windowInsetsControllerCompat.Hide(WindowInsetsCompat.Type.StatusBars() | WindowInsetsCompat.Type.NavigationBars());  
                windowInsetsControllerCompat.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;  
      
            }  
            #endregion  
      
            #region ShowSystemUi  
            private void ShowSystemUi()  
            {  
                WindowCompat.SetDecorFitsSystemWindows(Activity.Window, true);  
                windowInsetsControllerCompat.Show(WindowInsetsCompat.Type.StatusBars() | WindowInsetsCompat.Type.NavigationBars());  
                  
                if (Activity is MainActivity mainActivity)  
                {  
                    mainActivity.SupportActionBar.Show();  
                    mainActivity.EnableDrawerLayout();  
                }  
            }  
            #endregion  
      
        }