Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

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

$
0
0

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

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>