3

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.

123iamking
  • 2,387
  • 4
  • 36
  • 56
  • I think the first two lines mean that you use [Xamarin.Forms Theme](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/theming). That is cross-platform styling. Then only use android styles.xml if you need to override on Android for some reason. The remaining lines only apply if you *want* to do something in `styles.xml`. Which you don't. So ignore. – ToolmakerSteve Nov 03 '21 at 22:35
  • @ToolmakerSteve - "That is cross-platform styling." - but I couldn't find anyway to cross-platform-ly set the status-bar's color . As I understand the "Xamarin.Forms Theme" is just a simple predefined color table so that when we switch theme, the app just reference the value of theme's table, it doesn't provide a way to set the color of status-bar (if I understand correctly). – 123iamking Nov 08 '21 at 05:41
  • I don't know the specifics (whether there is a cross-platform way to do what you want). I was just responding to the questions you raised in "Update" section. If its not an option in the theme, then you still have to do it in android code, as per the options you already mentioned. – ToolmakerSteve Nov 08 '21 at 18:33

0 Answers0