Xamarin Forms - How to set status bar color in Xamarin.Forms 4.6?

Viewed 266

I have found several methods how to set Status Bar color:

Method #1: using C#

In Android:

-> The Android project

-> File MainActivity.cs

-> In the Function protected override void OnCreate(Bundle savedInstanceState)

-> At the end of that function (right after the code LoadApplication(new App());), Add the code Window.SetStatusBarColor(Android.Graphics.Color.Argb(255, 0, 0, 0));

Note: SetStatusBarColor is only supported in API level 21 and up. Therefore, we should check for this before calling SetStatusBarColor. if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop){Window.SetStatusBarColor(...);}

But I don't like this method, to set color & theme stuff, I prefer xaml style file. Beside, when the app load, I see that the app load to default color (blue) then change to my color (black) - I don't like that.

So I would like to go with the style file method which is Method #2:

Method #2: Style file

In Android:

-> The Android project

-> File \Resources\values\styles.xml

-> Set <item name="colorPrimaryDark">#00FF00</item>

When I open the file \Resources\values\styles.xml (Android project), I see:

<style name="MainTheme" parent="MainTheme.Base">
    <!-- As of Xamarin.Forms 4.6 the theme has moved into the Forms binary -->
    <!-- If you want to override anything you can do that here. -->
    <!-- Underneath are a couple of entries to get you started. -->

    <!-- Set theme colors from https://aka.ms/material-colors -->
    <!-- colorPrimary is used for the default action bar background -->
    <!--<item name="colorPrimary">#2196F3</item>-->
    <!-- colorPrimaryDark is used for the status bar -->
    <!--<item name="colorPrimaryDark">#1976D2</item>-->
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <!--<item name="colorAccent">#FF4081</item>-->
  </style>

It said:

... Xamarin.Forms 4.6 ... theme ... moved into the Forms binary ... do that here.

So where is the "Forms binary" that I can set the status bar color? How exactly I can "override" it and "do that there"? I hope it's in the Xamarin Shared Project so that I can globally set status bar color for all platforms (I'm a lazy guy, I don't want to change status bar color in each platform project. My project is pretty simple, it doesn't have light theme/dark theme, just simply set status bar color for all platforms is good enough for me)

Update:

Wait a minute, maybe I understand it wrong, it said "do that here. (not there)", so maybe in the past, it was done somewhere else. So I just need to to put the code <item name="colorPrimaryDark">#353a3e</item> in the styles.xml (Android) to set the status bar color.

But I really hope there is a way to set the status bar color in the shared project so that I don't have to change the status bar color in each project.

0 Answers
Related