Forum Xamarin.Forms

How to set Xamarin.Forms StatusBarColor

m_y_tahirm_y_tahir Member ✭✭
edited May 2020 in Xamarin.Forms
Hello, I am havin issues trying to add StatusBarColor to my Android Project, but initially I am XF.Material Library but totally confused on how to implement StatusBarColor on it, I am using Android 5.1 Physical Device to test my App.

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited May 2020 Accepted Answer

    You could define MaterialNavigationPage.StatusBarColor insdie ContenPage .

        <ContentPage .... xmlns:material="clr-namespace:XF.Material.Forms.UI;assembly=XF.Material"
                   material:MaterialNavigationPage.StatusBarColor="#1B3147" ></ ContentPage>
    

    https://github.com/Baseflow/XF-Material-Library#app-bar-customization .

  • RainerMagerRainerMager JPMember ✭✭✭

    I just struggled with this myself and finally got it working.
    In my Android project I have:

    using Android.OS;
    using BeerPlease.App.Droid;
    using BeerPlease.App.Forms;
    using Plugin.CurrentActivity;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: Dependency(typeof(StatusBarRenderer))]
    namespace BeerPlease.App.Droid
    {
        public class StatusBarRenderer : IStatusBarPlatformSpecific
        {
            public StatusBarRenderer()
            {
            }
    
            public void SetStatusBarColor(Color color)
            {
                if(Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
                    var androidColor = color.AddLuminosity(-0.1).ToAndroid();
                    var window = CrossCurrentActivity.Current.Activity.Window;
                    window.SetStatusBarColor(androidColor);
                    // See https://developer.android.com/reference/android/view/View#SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    window.DecorView.SystemUiVisibility = color.Luminosity > 0.5
                        ? (Android.Views.StatusBarVisibility)8192
                        : Android.Views.StatusBarVisibility.Visible;
                }
            }
        }
    }
    

    This uses the CurrentActivity plugin, which makes it easy to get the current activity in Android from anywhere.

    Then in your XF project add something like this:

    using Xamarin.Forms;
    
    namespace BeerPlease.App.Forms
    {
        public interface IStatusBarPlatformSpecific
        {
            void SetStatusBarColor(Color color);
        }
    }
    

    Finally, also in your XF project add something like this to your main App class, the one that extends Xamarin.Forms.Application:

                    if(CurrentApp.Resources.TryGetValue("ColorPageBackground", out var color)) {
                        var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
                        statusbar?.SetStatusBarColor((Color)color);
                    }
    

    In my case I'm getting the color from my Resources, but obviously you could just hard code it too.

Sign In or Register to comment.