Search code examples
c#androidandroid-actionbarstatusbarmaui

How to make Status Bar fully transparent in .NET MAUI VISUAL STUDIOS 2022


So I was able to find that you can use depricated code to make the status bar on Android fully transparent:

    var s = SystemUiFlags.LayoutFullscreen | SystemUiFlags.LayoutStable;
    FindViewById(Android.Resource.Id.Content).SystemUiVisibility = (StatusBarVisibility)s;
    Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
    WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(Window, Window.DecorView);
    windowInsetsController.AppearanceLightStatusBars = true;

Since this code is deprecated, how do I achieve the same effect of the top status bar being fully transparent, as matching the background color of whatever picture or color is on the screen at the time. I am using .NET MAUI in VS 2022.


Solution

  • Code that should be on the page:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    #if ANDROID
        MainActivity.SetStatusBarLight(false);
    #endif
    }
    

    Code in MainActivity.cs

        protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Platform.Init(this, savedInstanceState);
    
        var s = SystemUiFlags.LayoutFullscreen | SystemUiFlags.LayoutStable;
    #pragma warning disable CS0618 // Type or member is obsolete
        FindViewById(Android.Resource.Id.Content).SystemUiVisibility = 
        (StatusBarVisibility)s;
    #pragma warning restore CS0618 // Type or member is obsolete
        Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
        WindowInsetsControllerCompat windowInsetsController = new 
        WindowInsetsControllerCompat(Window, Window.DecorView);
        windowInsetsController.AppearanceLightStatusBars = false;
    
        MainActivityInstance = this;
    }
    
        public static void SetStatusBarLight(bool isLight)
    {
        if (MainActivityInstance != null)
        {
            WindowInsetsControllerCompat windowInsetsController =
            new WindowInsetsControllerCompat(MainActivityInstance.Window, MainActivityInstance.Window.DecorView);
            windowInsetsController.AppearanceLightStatusBars = isLight;
        }
    }