4

I have created a Xamarin Forms project and i am unable to change my Android status bar color to transparent. I am changing my colors programmatically in the OnCreate() method of my MainActivity as follow:

  if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
            Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            Window.SetStatusBarColor(Color.Transparent);
        }

I tried different colors and it's working fine but "Transparent" is not working at all. I am testing on an API 22.

My style.xml is as follow:

  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/blue</item>
    <item name="colorAccent">#FF4081</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>

If i set programmatically my color to Purple for example it will work without issue, but if I set it to Transparent, i will get my style.xml color which is blue. If i remove my style.xml ColorPrimaryDark color, i get a gray status bar. What could be the solution?

Huby03
  • 801
  • 2
  • 14
  • 28
  • 2
    Possible duplicate of [Lollipop : draw behind statusBar with its color set to transparent](http://stackoverflow.com/questions/27856603/lollipop-draw-behind-statusbar-with-its-color-set-to-transparent) – Gerald Versluis May 05 '17 at 07:07

4 Answers4

5

Did you try this ?

<item name="android:windowTranslucentStatus">true</item>
4

You can try adding this in styles.xml (Android proj)

<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="android:statusBarColor">@android:color/transparent</item>
   <item name="android:windowTranslucentStatus">true</item>
</style>

Looks something like this:-

enter image description here

user10398433
  • 426
  • 3
  • 10
1

To achieve this please put that code before LoadApplication(new App()) in OnCreate() Method of your Xamarin.Android Project's MainActivity class.

private void TransparentStatusBar()
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
    {
        // for covering the full screen in android..
        Window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);

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

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

        Window.DecorView.SystemUiVisibility = 0;

        Window.SetStatusBarColor(Android.Graphics.Color.Transparent);

    }

}
stud3nt
  • 2,056
  • 1
  • 12
  • 21
0

Use below mentioned code. That has worked for me perfectly.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            Window.DecorView.SystemUiVisibility = 0;
            var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            statusBarHeightInfo.SetValue(this, 0);
            Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
        }

        LoadApplication(new App());
    }
}
Nirav Shah
  • 2,064
  • 5
  • 24
  • 34