Stefano
Stefano

Reputation: 210

Xamarin forms: Customize Android Material style status bar color

I have enabled Material theme on my Xamarin.Forms project with the following code, but on Android (Lollipop and Marshmallow) the top status bar (where the clock, signal, battery, etc.. are located) doesn't change. It remains always black. I've already read lot of forums and blogs, tried lot of combination but I'm not able to get this status bar colored as I want.

MainActivity.cs

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

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>

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!

Upvotes: 4

Views: 7097

Answers (4)

Gustavo Baiocchi Costa
Gustavo Baiocchi Costa

Reputation: 1448

I had the same as the accepted answer. It was working on one of our projects but not the other.

I have not seen a solution to this particular problem so I am being the first here.

If you have declared your uses-permissions in the assemblyInfo.cs instead of the manifest will cause the status bar not to work as expected.

Even if you get it working by setting it individually for each activity it still doesn't work when you are transitioning to activities. So the only way is to move the uses-permission statements to the manifest.

Hopefully I can help someone!

Upvotes: 0

Joseph
Joseph

Reputation: 6031

A translation from the equivalent Java version :

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)));
}

Upvotes: 1

Benoit Canonne
Benoit Canonne

Reputation: 455

Other solution most simply, in method 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));

Upvotes: 4

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

You should follow this Tutorial to get success.

Basically you have to use FormsAppCompatActivity instead of FormsApplicationActivity and Theme.AppCompat.Light.NoActionBar instead of Theme.Material.Light.

Upvotes: 4

Related Questions