0

In the MainActivity OnCreate, I set the color of the StatusBar using:

Window.SetStatusBarColor(Resources.GetColor(Resource.Color.colorPrimary));

For the specific pages, I need to set the StatusBar color trasparent.

Is possible to do that in a Android custom rendered class?

EDIT: my OnLayout method on custom ANdorid

 protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {

            CustomNavigation.IgnoreLayoutChange = true;
            base.OnLayout(changed, l, t, r, b);
            CustomNavigation.IgnoreLayoutChange = false;

            int containerHeight = b - t;

            PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));


            if (Element?.Navigation?.NavigationStack.Count == 1)
            {
                CustomNavigation.BarBackgroundColor = Color.Transparent;
                //HERE I NEED TO HAVE STATUS AR TRANSPARENT 
            }


            if (Element?.Navigation?.NavigationStack.Count > 1)
            {
                PageController.ContainerArea = new Rectangle(0, 60, Context.FromPixels(r - l), Context.FromPixels(containerHeight));
                CustomNavigation.BarBackgroundColor = Color.FromHex("#006CA6");
            }


            for (var i = 0; i < ChildCount; i++)
            {
                AView child = GetChildAt(i);

                if (child is Android.Support.V7.Widget.Toolbar)
                {

                   continue;
                }

                child.Layout(0, 0, r, b);
            }
        }
doxsi
  • 1,002
  • 19
  • 42

1 Answers1

1

Status bar appearance is about its background and text colours. Both properties have their own limitations on different platforms, however, we could manipulate both with the solution described below.

Our goal is simple, we want to be able to switch the status bar appearance between LightTheme and DarkTheme at runtime:

  • Define an interface in your shared code:

     public interface IStatusBarStyleManager
     {
          void SetLightTheme();
          void SetDarkTheme();
     }
    

Since Android Lollipop (21) it is possible to set a custom status bar background colour by simply defining it in style.xml with a key colorPrimaryDark or programmatically, Since Android M (23) it is possible to set a predefined status bar text colour theme to light or dark.

Android code:

   public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetDarkTheme()
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var currentWindow = GetCurrentWindow();
            currentWindow.DecorView.SystemUiVisibility = 0;
            currentWindow.SetStatusBarColor(Android.Graphics.Color.DarkCyan);
        });
    }
}

public void SetLightTheme()
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var currentWindow = GetCurrentWindow();
            currentWindow.DecorView.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.LightStatusBar;
            currentWindow.SetStatusBarColor(Android.Graphics.Color.LightGreen);
        });
    }
}

Window GetCurrentWindow()
{
    var window = CrossCurrentActivity.Current.Activity.Window;

    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.ClearFlags(WindowManagerFlags.TranslucentStatus);

    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

    return window;
}
}

I am using the Current Activity Plugin by James Montemagno to get the reference of the current activity.

iOS code:

In iOS the status bar background colour by default matching the colour of the navigation bar. In other words, we don’t have to explicitly set the background colour of the status bar if we want it to match the background colour of the navigation bar. Since iOS 7 it is possible to set a predefined status bar text colour theme to light or dark. However, we will have to manipulate the Info.plist. Since status bar behaviour is determined by view controllers by default, we have to disable this:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

Next, we can define a default text colour theme:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>


public class StatusBarStyleManager : IStatusBarStyleManager
{
    public void SetDarkTheme()
   {
        Device.BeginInvokeOnMainThread(() =>
       {
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
        GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
    });
}

public void SetLightTheme()
{
    Device.BeginInvokeOnMainThread(() =>
    {
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.Default, false);
        GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
    });
}

    UIViewController GetCurrentViewController()
   {
      var window = UIApplication.SharedApplication.KeyWindow;
      var vc = window.RootViewController;
      while (vc.PresentedViewController != null)
          vc = vc.PresentedViewController;
      return vc;
   }
 }

Goodluck

Revert in case of queries.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63