Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Hide StatusBar and Notification in Fullscreen mode

I am trying to hide statusbar and notification in fullscreen mode. User should not be able to pull/swipe down status/notification bar in Xamarin android.

In native android it is working fine with below property.

View decorView = getWindow().getDecorView(); 
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

getWindow().setFlags(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);

Equivalent to TYPE_APPLICATION_OVERLAY in xamarin not able to get.

Any solution for Xamarin.Android?

I have tried below properties for xamarin:

View decorView = Window.DecorView;
    var uiOptions = (int)decorView.SystemUiVisibility;
    var newUiOptions = (int)uiOptions;      
    newUiOptions |= (int)SystemUiFlags.HideNavigation;
    newUiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
    newUiOptions |= (int)SystemUiFlags.LayoutFullscreen;
    newUiOptions |= (int)SystemUiFlags.Fullscreen;
    newUiOptions |= (int)SystemUiFlags.ImmersiveSticky; 
    decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;

    Window.AddFlags(WindowManagerFlags.TranslucentNavigation);
    Window.AddFlags(WindowManagerFlags.TranslucentStatus);
    Window.ClearFlags(WindowManagerFlags.ForceNotFullscreen);

Help will be appreciated. Thank you

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    @Raheem
    Could you please using the following code and try again?

    Call method:          immersive(this.Window, 0, 0);
    public void immersive(Window window, int color, float alpha)
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        { // 21
            window.ClearFlags(WindowManagerFlags.TranslucentStatus);
            window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            window.SetStatusBarColor(new Android.Graphics.Color(mixtureColor(color, alpha)));
    
            SystemUiFlags systemUiVisibility = (SystemUiFlags)window.DecorView.SystemUiVisibility;
            systemUiVisibility |= SystemUiFlags.LayoutFullscreen;
            systemUiVisibility |= SystemUiFlags.LayoutStable;
            window.DecorView.SystemUiVisibility = (StatusBarVisibility)systemUiVisibility;
        }
        else if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
        { // 19
            window.AddFlags(WindowManagerFlags.TranslucentStatus);
            setTranslucentView((ViewGroup)window.DecorView, color, alpha);
        }
        else if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBean)
        { // 16
            SystemUiFlags systemUiVisibility = (SystemUiFlags)window.DecorView.SystemUiVisibility;
            systemUiVisibility |= SystemUiFlags.LayoutFullscreen;
            systemUiVisibility |= SystemUiFlags.LayoutStable;
            window.DecorView.SystemUiVisibility = (StatusBarVisibility)systemUiVisibility;
        }
    }
    
    public static int mixtureColor(int color, float alpha)
    {
        int a = (color & 0x000000) == 0 ? 0xff : color >> 24;
        return (color & 0xffffff) | (((int)(a * alpha)) << 24);
    }
    
    /** create a fake transparent bar*/
    public static void setTranslucentView(ViewGroup container, int color, float alpha)
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
        { // 19
            int _mixtureColor = mixtureColor(color, alpha);
            View translucentView = container.FindViewById(Android.Resource.Id.Custom);
            if (translucentView == null && _mixtureColor != 0)
            {
                translucentView = new View(container.Context);
                translucentView.Id = Android.Resource.Id.Custom;
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MatchParent, getStatusBarHeight(container.Context));
                container.AddView(translucentView, lp);
            }
            if (translucentView != null)
            {
                translucentView.SetBackgroundColor(new Android.Graphics.Color(_mixtureColor));
            }
        }
    }
    
    /** get the height of statusbar */
    public static int getStatusBarHeight(Context context)
    {
        int result = 24;
       int resId = context.Resources.GetIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0)
        {
            result = context.Resources.GetDimensionPixelSize(resId);
        }
        else
        {
            result = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip,
                    result, Android.Content.Res.Resources.System.DisplayMetrics);
        }
        return result;
    }
    
Sign In or Register to comment.