summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/ResourcesProvider.cs
diff options
context:
space:
mode:
authorJason Smith <jason.smith@xamarin.com>2016-03-22 13:02:25 -0700
committerJason Smith <jason.smith@xamarin.com>2016-03-22 16:13:41 -0700
commit17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch)
treeb5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/ResourcesProvider.cs
downloadxamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2
xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.Android/ResourcesProvider.cs')
-rw-r--r--Xamarin.Forms.Platform.Android/ResourcesProvider.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/ResourcesProvider.cs b/Xamarin.Forms.Platform.Android/ResourcesProvider.cs
new file mode 100644
index 00000000..334ef56a
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/ResourcesProvider.cs
@@ -0,0 +1,68 @@
+using Android.Content;
+using Android.Content.Res;
+using Android.Util;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ internal class ResourcesProvider : ISystemResourcesProvider
+ {
+ ResourceDictionary _dictionary;
+
+ public IResourceDictionary GetSystemResources()
+ {
+ _dictionary = new ResourceDictionary();
+
+ UpdateStyles();
+
+ return _dictionary;
+ }
+
+ public Style GetStyle(int style)
+ {
+ var result = new Style(typeof(Label));
+
+ double fontSize = 0;
+ string fontFamily = null;
+ global::Android.Graphics.Color defaultColor = global::Android.Graphics.Color.Argb(0, 0, 0, 0);
+ global::Android.Graphics.Color androidColor = defaultColor;
+
+ Context context = Forms.Context;
+ using(var value = new TypedValue())
+ {
+ if (context.Theme.ResolveAttribute(style, value, true))
+ {
+ var styleattrs = new[] { global::Android.Resource.Attribute.TextSize, global::Android.Resource.Attribute.FontFamily, global::Android.Resource.Attribute.TextColor };
+ using(TypedArray array = context.ObtainStyledAttributes(value.ResourceId, styleattrs))
+ {
+ fontSize = context.FromPixels(array.GetDimensionPixelSize(0, -1));
+ fontFamily = array.GetString(1);
+ androidColor = array.GetColor(2, defaultColor);
+ }
+ }
+ }
+
+ if (fontSize > 0)
+ result.Setters.Add(new Setter { Property = Label.FontSizeProperty, Value = fontSize });
+
+ if (!string.IsNullOrEmpty(fontFamily))
+ result.Setters.Add(new Setter { Property = Label.FontFamilyProperty, Value = fontFamily });
+
+ if (androidColor != defaultColor)
+ {
+ result.Setters.Add(new Setter { Property = Label.TextColorProperty, Value = Color.FromRgba(androidColor.R, androidColor.G, androidColor.B, androidColor.A) });
+ }
+
+ return result;
+ }
+
+ void UpdateStyles()
+ {
+ _dictionary[Device.Styles.BodyStyleKey] = new Style(typeof(Label)); // do nothing, its fine
+ _dictionary[Device.Styles.TitleStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceLarge);
+ _dictionary[Device.Styles.SubtitleStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceMedium);
+ _dictionary[Device.Styles.CaptionStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceSmall);
+ _dictionary[Device.Styles.ListItemTextStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceListItem);
+ _dictionary[Device.Styles.ListItemDetailTextStyleKey] = GetStyle(global::Android.Resource.Attribute.TextAppearanceListItemSmall);
+ }
+ }
+} \ No newline at end of file