Cannot truely hide the Status Bar on Android!

Edom90Edom90 USMember ✭✭

I'm working on a Xamarin.Forms project and I've just found the way to hide the icons from the status bar with this code:
this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);

Now i'm trying to hide the blue status bar, and I've tried 2 different ways, both not working:
1) Add this code to the activity.cs:
SetStatusBarColor(Color.Transparent);

2) Write these lines in all the possible combinations inside the style.xml (in Resources/value):
item name="colorPrimary">@android:color/transparent
item name="android:windowDrawsSystemBarBackgrounds">true
item name="android:windowTranslucentStatus">true
item name="android:windowTranslucentNavigation">true

Nothing seems to work, What should I do?

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @EdoardoFedericoMastrandrea said:
    I'm working on a Xamarin.Forms project and I've just found the way to hide the icons from the status bar with this code:
    this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);

    How are you using that line: It throws an error. You can't do a bitwise or on that.
    image
    Try it without the second flag.
    this.Window.AddFlags(WindowManagerFlags.Fullscreen);

  • Edom90Edom90 USMember ✭✭
    edited December 2016

    @ClintStLaurent I used it in the MainActivity as i said, inside the OnCreate method, without any error.
    i think that your "this" (in this.window.addflags....) is not pointing the right place. should be like:
    YourNamespace.Droid.YourActivity...

    EDIT:
    I've seen just now that you put one more closing bracket ")" after the property ".fullscreen"

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @EdoardoFedericoMastrandrea said:
    I've seen just now that you put one more closing bracket ")" after the property ".fullscreen"

    Ah... Good eyes! You are correct that fixed the error.

    The interesting thing is... The app I'm working on is full screen, no status bar and has been for months. I'm pretty sure that same line is the only line we've added to make this happen. If there is something else done in the last 9 months to make this full screen I don't remember what it was, nor see it in the manifest, activity attributes etc. It sure looks to me like that was all we did.

    When I comment out that line, and re-run the app it once again shows the status bar;
    Uncomment the line, re-run, status bar is gone.
    It really does seem like that's all that I needed.

    Is it possible that in all your trials you've added something else that re-activates the status bar?
    If you start with a brand new "Welcome to Xamarin" solution, and add that one line to the main activity does it loose the status bar?

    Maybe you also have to specifiy you have permission to change the screensize?
    [Activity(ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,

  • Edom90Edom90 USMember ✭✭

    @ClintStLaurent said:

    @EdoardoFedericoMastrandrea said:
    I've seen just now that you put one more closing bracket ")" after the property ".fullscreen"

    Ah... Good eyes! You are correct that fixed the error.

    The interesting thing is... The app I'm working on is full screen, no status bar and has been for months. I'm pretty sure that same line is the only line we've added to make this happen. If there is something else done in the last 9 months to make this full screen I don't remember what it was, nor see it in the manifest, activity attributes etc. It sure looks to me like that was all we did.

    When I comment out that line, and re-run the app it once again shows the status bar;
    Uncomment the line, re-run, status bar is gone.
    It really does seem like that's all that I needed.

    Is it possible that in all your trials you've added something else that re-activates the status bar?
    If you start with a brand new "Welcome to Xamarin" solution, and add that one line to the main activity does it loose the status bar?

    Maybe you also have to specifiy you have permission to change the screensize?
    [Activity(ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,

    No thanks.. It's the first thing i tried. =(

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Plan 1 - I just confirmed this with a new project
    Looking at the attributes to your MainActivity.cs -
    Change your inheritence on the MainActivity class. to FormsApplicationActivity
    That makes the new MainActivity.cs file look like this:

    using System;
    
    using Android.App;
    using Android.Content.PM;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Xamarin.Forms.Platform.Android;
    
    namespace MyCoolApp.Droid
    {
        [Activity(ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, 
            Label = "MyCoolApp", Icon = "@drawable/icon", 
            WindowSoftInputMode = SoftInput.AdjustPan,
            MainLauncher = true)]
        //Theme = "@style/MainTheme",
        public class MainActivity : FormsApplicationActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                //TabLayoutResource = Resource.Layout.Tabbar;
                //ToolbarResource = Resource.Layout.Toolbar;
    
                base.OnCreate(bundle);
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
                LoadApplication(new App());
                this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);
    
            }
        }
    }
    

    The downside of this is that you can't assign a Material Theme or it causes a NullReferenceException. If you're not going to use a theme, then you don't care. Which is where I was for the longest time because all the themeing is being done in our PCL not in the Android platform.
    However, because I want to assign a splashscreen, and that is done in a theme... I'm once again banging on this problem of having EVERYTHING: Theme, splashscreen, and no status bar.

    In a thread that was started a while back a couple of us are once again tackling both Theme and NoTitleBar
    https://forums.xamarin.com/discussion/83566/android-xamarin-forms-set-splash-screen-while-xamarin-forms-initalizes

  • Edom90Edom90 USMember ✭✭

    @AdamMeaney said:
    If you add

    if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
    {
        // Kill status bar underlay added by FormsAppCompatActivity
        // Must be done before calling FormsAppCompatActivity.OnCreate()
        var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (statusBarHeightInfo == null)
        {
            statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        }
        statusBarHeightInfo?.SetValue(this, 0);
    }
    

    before the base.OnCreate of the activity, then have

    this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);

    at the end as shown above, I get it full screen with the extra status bar removed.

    Xamarin includes an underlay, as shown in this github file, and it needs to be told not to be there.

    THANK YOU @AdamMeaney . Finally it works. Great :smiley:

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited December 2016

    @Edom90 @AdamMeaney
    Thanks for sharing that!

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @Edom90 In 2.3.4-pre1, you should be able to hide the status bar. There is another PR to make status bar/keyboard experience better: https://github.com/xamarin/Xamarin.Forms/pull/422. Please comment so it can be reviewed.

Sign In or Register to comment.