Xamarin Forms: Adjust the color of the status bar of Android material

I included the material topic in my Xamarin.Forms project with the following code, but on Android (Lollipop and Marshmallow) the top status bar (where the clock, signal, battery, etc.) is located, it always remains black. I already read a lot of forums and blogs, tried to combine many times, but I can’t get this status bar in different ways as I want.

MainActivity.cs

[Activity(Theme = "@style/MyTheme", Label = "MyApp", Icon = "@drawable/AppIcon", ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

<strong> values-V21 / style.xml

<resources>
  <style name="MyTheme.Splash" parent="android:Theme.Material.Light">    
    <item name="android:windowBackground">@drawable/SplashScreen</item>
    <item name="android:windowNoTitle">true</item>
  </style>
  <style name="MyTheme" parent="android:Theme.Material.Light">
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:colorPrimary">@color/Brand</item>
    <item name="android:textColorPrimary">@color/White</item>
    <item name="android:statusBarColor">@color/BrandDark</item>
    <item name="android:colorPrimaryDark">@color/BrandDark</item>
    <item name="android:colorAccent">@color/Brand</item>
    <item name="android:textColor">@color/Brand</item>    
  </style>  
</resources>

<strong> values ​​/colors.xml

<resources>  
  <color name="SplashBackground">#f78b2b</color>
  <color name="Brand">#f78b2b</color>
  <color name="BrandDark">#e47108</color>
  <color name="White">#ffffff</color>
</resources>

Thanks for any help!

+4
source share
4 answers

, .

FormsAppCompatActivity FormsApplicationActivity Theme.AppCompat.Light.NoActionBar Theme.Material.Light.

+4

, OnCreate of MainActivity.cs add:

Window window = this.Window; window.ClearFlags(WindowManagerFlags.TranslucentStatus); window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds); window.SetStatusBarColor(Android.Graphics.Color.Rgb(116, 181, 13));

+4

Java:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
    // clear FLAG_TRANSLUCENT_STATUS flag:
    Window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);

    //Window.ClearFlags(WindowManager.Pa WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    Window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);

    // finally change the color
    Window.SetStatusBarColor(new Color(ContextCompat.GetColor(this, Resource.Color.colorPrimaryDark)));
}
+1

, . , .

, .

- assemblyInfo.cs , , .

, , , . , - - .

Hope I can help someone!

0
source

All Articles