How to make Color.Accent match your Android app's theme

JoeMankeJoeManke USMember ✭✭✭✭✭
edited April 2016 in Xamarin.Forms

As you may be aware, Xamarin.Forms' Color.Accent is a static, internally set property for use throughout your Xamarin.Forms app. Unfortunately, Color.Accent does not care about the accent color you set in your app's Material Design theme. If you would like Color.Accent to match your theme, this is a code snippet I developed, using reflection to access the internal setter. You want to place it in your MainActivity's OnCreate() method, after Xamarin.Forms.Forms.Init() has been called, as that is where Color.Accent is originally set to Holo Blue. Enjoy.

protected override void OnCreate(Bundle bundle)
{
    global::Xamarin.Forms.Forms.Init(this, bundle)

    // get the accent color from your theme
    var themeAccentColor = new TypedValue();
    this.Theme.ResolveAttribute(Resource.Attribute.colorAccent, themeAccentColor, true);
    var droidAccentColor = new Android.Graphics.Color(themeAccentColor.Data);

    // set Xamarin Color.Accent to match the theme's accent color
    var accentColorProp = typeof(Xamarin.Forms.Color).GetProperty(nameof(Xamarin.Forms.Color.Accent), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
    var xamarinAccentColor = new Xamarin.Forms.Color(droidAccentColor.R / 255.0, droidAccentColor.G / 255.0, droidAccentColor.B / 255.0, droidAccentColor.A / 255.0);
    accentColorProp.SetValue(null, xamarinAccentColor, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, null, null, System.Globalization.CultureInfo.CurrentCulture);

    LoadApplication(new App());
}

Posts

  • FelixCollinsFelixCollins NZMember

    Thank you!

  • BjornBBjornB USMember ✭✭✭

    On what places does this default holo.blue effect my UI (on places the Accent in my material theme doesnt)?

    And thank you!

  • JayKappelJayKappel USMember ✭✭

    I am trying to use this method in order to change the color of the selected tab indicator in a Xamarin forms app. I'm not getting any errors, but it's not changing the color either. Here's what I have in my Android project MainActivity.cs:

            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
                global::Xamarin.Forms.Forms.Init(this, bundle);
    
                SetAccentColor();
                LoadApplication(new App(new OperatingSystemAccess(this)));            
            }
    
            private void SetAccentColor()
            {
                // set Xamarin Color.Accent 
                var xamarinAccentColor = Xamarin.Forms.Color.FromHex("#026937"); // Green
                var accentColorProp = typeof(Xamarin.Forms.Color).GetProperty(nameof(Xamarin.Forms.Color.Accent), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
                accentColorProp.SetValue(accentColorProp, xamarinAccentColor, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static, null, null, System.Globalization.CultureInfo.CurrentCulture);
            }
    

    What am I missing?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You mean the indicator on the TabbedPage? That isn't set to Color.Accent anywhere in code, it's determined by the tabIndicatorColor property in your tab_layout.xml file. Default is the accent color set on your theme.

  • RD8RD8 USMember
    edited October 2016

    Thanks Joe.

    In XAML I'm seeing the accent color pull through from the Android styles.xml
    It works for BoxView background color and a Label's text color.
    Perhaps this is included as part of Xamarin.Forms.Platform.Android.FormsAppCompatActivity ?

    <BoxView BackgroundColor="Accent" HeightRequest="200" WidthRequest="200"></BoxView> <Frame OutlineColor="Accent" HorizontalOptions="Center"> <Label Text="false" TextColor="Accent" /> </Frame>

    I didn't have the same luck with Primary or PrimaryDark, trying to reference those throws an exception.

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited October 2016

    In XAML you're referencing Xamarin.Forms.Color.Accent. My code snippet is just a hack to set that value to what you want. There are no analogs to Android's Primary or PrimaryDark in Forms. If you want to explicitly use those colors in your cross-platform XAML, I would recommend adding some colors to your App Resources in App.xaml.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Just a note, since this managed to slip by me for two months - this snippet should no longer be necessary. Xamarin added code to set Color.Accent to the themed value in Xamarin.Forms 2.3.2.

  • Tro_SoloTro_Solo DEMember ✭✭

    I don't know since when, but apparently the automatic accent color detection feature is broken again: https://forums.xamarin.com/discussion/85896/android-color-accent-not-working. Good news is that a bug report has been filed and the Xamarin developers have confirmed this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=53222.

    Based on the code provided by @JoeManke I was able to implement an utility method which can be called from the constructor of your main Xamarin.Forms Application (PCL). I prefer to call this method after the initialization of the XAML part of the App class (this.InitializeComponent()), but even without using XAML entirely, this approach seems to work. My main reason to go this way is because I want to use my accent color on iOS, too.

    Long story short, here's the code:

    private static void ApplyAccentColor(Color newAccentColor) { // Access the setter of the 'Xamarin.Forms.Color.Accent' property var prop = typeof(Color).GetRuntimeProperty(nameof(Color.Accent)); var setter = prop?.SetMethod; // Invoke the setter method of the Color.Accent property to overwrite it with the custom accent color setter?.Invoke(Color.Accent, new[] { newAccentColor }); }

  • guilhermemaiaguilhermemaia USMember

    is this really working? my code didn't compile. I had to change it to "setter?.Invoke(Color.Accent, new object[] { newAccentColor });" but it didn't work.

    Am i missing something?

    @Tro_Solo said:
    I don't know since when, but apparently the automatic accent color detection feature is broken again: https://forums.xamarin.com/discussion/85896/android-color-accent-not-working. Good news is that a bug report has been filed and the Xamarin developers have confirmed this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=53222.

    Based on the code provided by @JoeManke I was able to implement an utility method which can be called from the constructor of your main Xamarin.Forms Application (PCL). I prefer to call this method after the initialization of the XAML part of the App class (this.InitializeComponent()), but even without using XAML entirely, this approach seems to work. My main reason to go this way is because I want to use my accent color on iOS, too.

    Long story short, here's the code:

    private static void ApplyAccentColor(Color newAccentColor) { // Access the setter of the 'Xamarin.Forms.Color.Accent' property var prop = typeof(Color).GetRuntimeProperty(nameof(Color.Accent)); var setter = prop?.SetMethod; // Invoke the setter method of the Color.Accent property to overwrite it with the custom accent color setter?.Invoke(Color.Accent, new[] { newAccentColor }); }

  • Tro_SoloTro_Solo DEMember ✭✭
    edited April 2017

    @guilhermemaia : Yey, this code really is working for me. But nonetheless you are right, I have a small error within my code because somehow the object got lost. :/ The last line should be:
    setter?.Invoke(Color.Accent, new object[] { newAccentColor });

    I have attached a very basic sample project using the PCL and XAML approach that I just created to show how I use this method. The sample app has been tested successfully on Android, iOS and UWP.
    Hope this helps.

  • jacko1394jacko1394 Member

    For anyone just looking to change the Android Accent Color value:

    Droid proj > Resources > values > style.xml
    

    Just change the hex value for "colorAccent"

  • Jenitha.2420Jenitha.2420 USMember ✭✭

    how to change the accent color for xamarin ios

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You can use the Color.SetAccent method from anywhere that has a Xamarin.Forms reference including your iOS project.

Sign In or Register to comment.