Temporarily hide Android controls and/or notifications bar temporarily in .net maui 7

Sebastian Amyotte 21 Reputation points
2022-12-11T06:42:29.187+00:00

Disclosure: I am a student, and this is for my final project.

I am pushing a page with Navigation.PushAsync()

I would like this page to open while hiding the bottom 3 Android buttons (Back, home, and tray), and the top notifications bar that shows the time, wifi connection, battery, etc.

This page shows an image in full screen, which is working. If the image is tapped anywhere, I am calling Navigation.pop() which returns me to the previous screen. This page would then show the Android buttons and top notifications bar once again.

In other words, I'd like this page to display as "fullscreen" as possible, and "un-fullscreen-ing" when popping the page. I am compiling with .net 7, and the device I am testing on has an Android API level of 33. I am only concerned with Android development, the other platforms supported by Maui are not considered.

This is my first question here, so If I am missing something necessary, let me know.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-12T05:25:21.113+00:00

    Hello,

    Firstly, you need to install CommunityToolkit.Mvvm nuget package. Then use WeakReferenceMessenger to control the full screen or normal screen with native android Apis. You can create two messages like following code in project.

       public class FullScreenMessage : ValueChangedMessage<object>  
       {  
           public FullScreenMessage(object r) : base(r)  
           {  
           }  
       }  
         
       public class NormalScreenMessage: ValueChangedMessage<object>  
       {  
           public NormalScreenMessage(object r) : base(r)  
           {  
           }  
       }  
    

    When Image page appears, you need to use full screen, when image page disappears, you should back to the normal screen. Please open your pages that contains Images, then override OnAppearing and OnDisappearing, send the message like following code.

       protected override void OnAppearing()  
       {  
               base.OnAppearing();  
              WeakReferenceMessenger.Default.Send(new FullScreenMessage("HideOsNavigationBar"));  
       }  
       protected override void OnDisappearing()  
       {  
               base.OnDisappearing();  
               WeakReferenceMessenger.Default.Send(new NormalScreenMessage("NormalNavigationBar"));  
       }  
    

    In the end, we need to Register the WeakReferenceMessenger, then execute the native android apis to set full screen or normal screen. Please open the MainActivity.cs(Open Platforms folder->Android folder->MainActivity.cs), then override the OnCreate method like following code.

       public class MainActivity : MauiAppCompatActivity  
       {  
         
         
         
          protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
         
               WeakReferenceMessenger.Default.Register<FullScreenMessage>(this, (r, m) =>  
               {  
                   IWindowInsetsController wicController = Window.InsetsController;  
                   Window.SetDecorFitsSystemWindows(false);  
                   Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);  
                  
                   if (wicController != null)  
                   {      
                       wicController.Hide(WindowInsets.Type.NavigationBars());  
                   }  
               });  
               WeakReferenceMessenger.Default.Register<NormalScreenMessage>(this, (r, m) =>  
               {  
                   IWindowInsetsController wicController = Window.InsetsController;  
                   Window.SetDecorFitsSystemWindows(true);  
                   Window.ClearFlags(WindowManagerFlags.Fullscreen);  
                  if (wicController != null)  
                   {            
                       wicController.Show(WindowInsets.Type.NavigationBars());  
                   }  
               });  
           }  
       }  
    

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.