hide status bar on xamarin.android

Issue

So I feel kinda dumb asking this but I really am confused right now. I need to hide the status bar when video goes fullscreen but I keep getting

CS0120 An object reference is required for the non-static field, method, or property ‘Activity.Window’

I tried making an instance of Activity class but that throws NullReferenceException.

Activity.Window.ClearFlags(WindowManagerFlags.Fullscreen); //to hide

So how do I use this method to hide status bar only when the video is in fullscreen?

 public class MainActivity : AppCompatActivity, BottomNavigationView.IOnNavigationItemSelectedListener
{
    WebView web_view;



    public class HelloWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            view.LoadUrl(url);
            return false;
        }
    }


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SupportActionBar.Hide();
        SetContentView(Resource.Layout.activity_main);

        var relativeLayout  FindViewById<RelativeLayout>(Resource.Id.container);
        var relativeLayoutTwo  FindViewById<RelativeLayout>(Resource.Id.containerTwo);
        web_view  FindViewById<WebView>(Resource.Id.webview);
        web_view.Settings.JavaScriptEnabled  true;
        web_view.SetWebChromeClient(new FullScreenClient(relativeLayout, relativeLayoutTwo));




        BottomNavigationView navigation  FindViewById<BottomNavigationView>(Resource.Id.navigation);
        navigation.SetOnNavigationItemSelectedListener(this);
    }

    public class FullScreenClient : WebChromeClient
    {
        readonly FrameLayout.LayoutParams matchParentLayout  new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
                                                                                                         ViewGroup.LayoutParams.MatchParent);
        readonly ViewGroup content;
        readonly ViewGroup parent;
        View customView;

        public FullScreenClient(ViewGroup parent, ViewGroup content)
        {
            this.parent  parent;
            this.content  content;
        }

        public override void OnShowCustomView(View view, ICustomViewCallback callback)
        {
            customView  view;
            view.LayoutParameters  matchParentLayout;
            parent.AddView(view);
            content.Visibility  ViewStates.Gone;
            **Activity.Window.ClearFlags(WindowManagerFlags.Fullscreen);**
        }

        public override void OnHideCustomView()
        {
            content.Visibility  ViewStates.Visible;
            parent.RemoveView(customView);
            customView  null;

        }
    }

Solution

I figured it out. I’ve used Xamarin.Essentials namespace and in each custom method I made a MainActivity class instance and set it equal to (MainActivity)Platform.CurrentActivity; and through that instance I was able to use Window.AddFlags(WindowManagerFlags.Fullscreen);

 MainActivity hideBar  new MainActivity();
            hideBar  (MainActivity)Platform.CurrentActivity;
            hideBar.Window.AddFlags(WindowManagerFlags.Fullscreen);

Answered By – magrega

Leave a Comment