summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-04-19 01:01:58 -0600
committerJason Smith <jason.smith@xamarin.com>2016-04-19 00:01:58 -0700
commita98f3fb2aae3c5b6a3fa5140e51f51ec67843a49 (patch)
tree654fa47cbd3cc889061466939630dcd5133cc837 /Xamarin.Forms.Platform.Android
parent89a50d4bc1f4aaf83d5f92a28c2a7a96282cc0d3 (diff)
downloadxamarin-forms-a98f3fb2aae3c5b6a3fa5140e51f51ec67843a49.tar.gz
xamarin-forms-a98f3fb2aae3c5b6a3fa5140e51f51ec67843a49.tar.bz2
xamarin-forms-a98f3fb2aae3c5b6a3fa5140e51f51ec67843a49.zip
Add TextColor Property to Picker, TimePicker, DatePicker (#84)
* Add TextColor property to TimePicker Add TextColor page to TimePicker gallery Add TimePicker color toggling to color toggle test page Split color toggle tests up among tabs * Implement TimePicker.TextColor in iOS * Implement TimePicker.TextColor for WinRT tablet * Add IsEnabled=false tests to DefaultColorToggleTest Button and TimePicker Consolidate ColorStates array Fix IsEnabled changing color bug on iOS * Implement TimePicker.TextColor for WP8 * Add TextColor property to DatePicker Add DatePicker section to DefaultColorToggleTest Impement DatePicker.TextColor for WP8 * Implement DatePicker.TextColor for WinRT/UWP/Windows Phone 8.1 * Implement DatePicker.TextColor for iOS * Add TextColor to DatePicker Gallery Page * Implement DatePicker.TextColor for Android * Add Picker Gallery page for TextColor Implement Picker.TextColor on Android Consolidate TextColor management for Button, Picker, DatePicker, TimePicker Implement * Add untracked TextColorSwitcher Implement Picker.TextColor in iOS * Implement Picker.TextColor in WinRT/UWP/Windows Phone 8.1 Remove Pioker Loaded handlers in Dispose * Implement Picker.TextColor in WP8 * Removed unused field Update ignored warnings * Update docs * Use nameof() for BindableProperties * Cleanup * Fix custom renderer crashes for classes using TextColorSwitcher * Correct property name references * Fix typo and 'if' formatting * Add missing else
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r--Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs33
-rw-r--r--Xamarin.Forms.Platform.Android/AppCompat/PickerRenderer.cs11
-rw-r--r--Xamarin.Forms.Platform.Android/ColorExtensions.cs6
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs21
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/DatePickerRenderer.cs17
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs13
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs15
-rw-r--r--Xamarin.Forms.Platform.Android/TextColorSwitcher.cs40
-rw-r--r--Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj7
9 files changed, 111 insertions, 52 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
index 3260dbf1..8f4ef35e 100644
--- a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
@@ -14,10 +14,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
public class ButtonRenderer : ViewRenderer<Button, AppCompatButton>, global::Android.Views.View.IOnAttachStateChangeListener
{
- static readonly int[][] States = { new[] { GlobalResource.Attribute.StateEnabled }, new[] { -GlobalResource.Attribute.StateEnabled } };
-
- ColorStateList _buttonDefaulTextColors;
- Color _currentTextColor;
+ TextColorSwitcher _textColorSwitcher;
float _defaultFontSize;
Typeface _defaultTypeface;
bool _isDisposed;
@@ -79,7 +76,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
button.SetOnClickListener(ButtonClickListener.Instance.Value);
button.Tag = this;
- _buttonDefaulTextColors = button.TextColors;
+ _textColorSwitcher = new TextColorSwitcher(button.TextColors);
SetNativeControl(button);
button.AddOnAttachStateChangeListener(this);
@@ -128,14 +125,16 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
Resources.Theme theme = context.Theme;
if (theme != null && theme.ResolveAttribute(id, value, true))
- Control.SupportBackgroundTintList = ContextCompat.GetColorStateList(context, value.Data);
+#pragma warning disable 618
+ Control.SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
+#pragma warning restore 618
else
- Control.SupportBackgroundTintList = new ColorStateList(States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
catch (Exception ex)
{
Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer", "Could not retrieve button background resource: {0}", ex);
- Control.SupportBackgroundTintList = new ColorStateList(States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
}
}
@@ -145,7 +144,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
int intColor = backgroundColor.ToAndroid().ToArgb();
int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
- Control.SupportBackgroundTintList = new ColorStateList(States, new[] { intColor, disableColor });
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
}
}
@@ -245,21 +244,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
void UpdateTextColor()
{
- Color color = Element.TextColor;
- if (color == _currentTextColor)
- return;
-
- _currentTextColor = color;
-
- if (color.IsDefault)
- NativeButton.SetTextColor(_buttonDefaulTextColors);
- else
- {
- // Set the new enabled state color, preserving the default disabled state color
- int defaultDisabledColor = _buttonDefaulTextColors.GetColorForState(States[1], color.ToAndroid());
-
- NativeButton.SetTextColor(new ColorStateList(States, new[] { color.ToAndroid().ToArgb(), defaultDisabledColor }));
- }
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
}
class ButtonClickListener : Object, IOnClickListener
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/PickerRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/PickerRenderer.cs
index dd7fb85c..492b553b 100644
--- a/Xamarin.Forms.Platform.Android/AppCompat/PickerRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/AppCompat/PickerRenderer.cs
@@ -2,6 +2,7 @@ using System;
using System.ComponentModel;
using System.Linq;
using Android.App;
+using Android.Content.Res;
using Android.Text;
using Android.Widget;
using Object = Java.Lang.Object;
@@ -12,6 +13,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
AlertDialog _dialog;
bool _disposed;
+ TextColorSwitcher _textColorSwitcher;
public PickerRenderer()
{
@@ -51,9 +53,11 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
textField.Tag = this;
textField.InputType = InputTypes.Null;
textField.SetOnClickListener(PickerListener.Instance);
+ _textColorSwitcher = new TextColorSwitcher(textField.TextColors);
SetNativeControl(textField);
}
UpdatePicker();
+ UpdateTextColor();
}
base.OnElementChanged(e);
@@ -67,6 +71,8 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
UpdatePicker();
if (e.PropertyName == Picker.SelectedIndexProperty.PropertyName)
UpdatePicker();
+ if (e.PropertyName == Picker.TextColorProperty.PropertyName)
+ UpdateTextColor();
}
internal override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
@@ -123,6 +129,11 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
Control.Text = Element.Items[Element.SelectedIndex];
}
+ void UpdateTextColor()
+ {
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
+ }
+
class PickerListener : Object, IOnClickListener
{
#region Statics
diff --git a/Xamarin.Forms.Platform.Android/ColorExtensions.cs b/Xamarin.Forms.Platform.Android/ColorExtensions.cs
index 2fee25a2..db190b4a 100644
--- a/Xamarin.Forms.Platform.Android/ColorExtensions.cs
+++ b/Xamarin.Forms.Platform.Android/ColorExtensions.cs
@@ -6,7 +6,7 @@ namespace Xamarin.Forms.Platform.Android
{
public static class ColorExtensions
{
- static readonly int[][] ColorStates = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } };
+ public static readonly int[][] States = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } };
public static AColor ToAndroid(this Color self)
{
@@ -33,8 +33,8 @@ namespace Xamarin.Forms.Platform.Android
public static ColorStateList ToAndroidPreserveDisabled(this Color color, ColorStateList defaults)
{
- int disabled = defaults.GetColorForState(ColorStates[1], color.ToAndroid());
- return new ColorStateList(ColorStates, new[] { color.ToAndroid().ToArgb(), disabled });
+ int disabled = defaults.GetColorForState(States[1], color.ToAndroid());
+ return new ColorStateList(States, new[] { color.ToAndroid().ToArgb(), disabled });
}
}
} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
index 43f042b1..7247361a 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
@@ -13,7 +13,7 @@ namespace Xamarin.Forms.Platform.Android
public class ButtonRenderer : ViewRenderer<Button, AButton>, AView.IOnAttachStateChangeListener
{
ButtonDrawable _backgroundDrawable;
- ColorStateList _buttonDefaulTextColors;
+ TextColorSwitcher _textColorSwitcher;
Drawable _defaultDrawable;
float _defaultFontSize;
Typeface _defaultTypeface;
@@ -78,7 +78,7 @@ namespace Xamarin.Forms.Platform.Android
button.SetOnClickListener(ButtonClickListener.Instance.Value);
button.Tag = this;
SetNativeControl(button);
-
+ _textColorSwitcher = new TextColorSwitcher(button.TextColors);
button.AddOnAttachStateChangeListener(this);
}
}
@@ -255,22 +255,7 @@ namespace Xamarin.Forms.Platform.Android
void UpdateTextColor()
{
- Color color = Element.TextColor;
-
- if (color.IsDefault)
- {
- if (_buttonDefaulTextColors == null)
- return;
-
- NativeButton.SetTextColor(_buttonDefaulTextColors);
- }
- else
- {
- _buttonDefaulTextColors = _buttonDefaulTextColors ?? Control.TextColors;
-
- // Set the new enabled state color, preserving the default disabled state color
- NativeButton.SetTextColor(color.ToAndroidPreserveDisabled(_buttonDefaulTextColors));
- }
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
}
class ButtonClickListener : Object, IOnClickListener
diff --git a/Xamarin.Forms.Platform.Android/Renderers/DatePickerRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/DatePickerRenderer.cs
index 11d9a0d0..5b0e0994 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/DatePickerRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/DatePickerRenderer.cs
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using Android.App;
+using Android.Content.Res;
using Android.Widget;
using AView = Android.Views.View;
using Object = Java.Lang.Object;
@@ -11,6 +12,7 @@ namespace Xamarin.Forms.Platform.Android
{
DatePickerDialog _dialog;
bool _disposed;
+ TextColorSwitcher _textColorSwitcher;
public DatePickerRenderer()
{
@@ -47,24 +49,28 @@ namespace Xamarin.Forms.Platform.Android
textField.SetOnClickListener(TextFieldClickHandler.Instance);
SetNativeControl(textField);
+ _textColorSwitcher = new TextColorSwitcher(textField.TextColors);
}
SetDate(Element.Date);
UpdateMinimumDate();
UpdateMaximumDate();
+ UpdateTextColor();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
- if (e.PropertyName == "Date" || e.PropertyName == DatePicker.FormatProperty.PropertyName)
+ if (e.PropertyName == DatePicker.DateProperty.PropertyName || e.PropertyName == DatePicker.FormatProperty.PropertyName)
SetDate(Element.Date);
- else if (e.PropertyName == "MinimumDate")
+ else if (e.PropertyName == DatePicker.MinimumDateProperty.PropertyName)
UpdateMinimumDate();
- else if (e.PropertyName == "MaximumDate")
+ else if (e.PropertyName == DatePicker.MaximumDateProperty.PropertyName)
UpdateMaximumDate();
+ if (e.PropertyName == DatePicker.TextColorProperty.PropertyName)
+ UpdateTextColor();
}
internal override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
@@ -141,6 +147,11 @@ namespace Xamarin.Forms.Platform.Android
}
}
+ void UpdateTextColor()
+ {
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
+ }
+
class TextFieldClickHandler : Object, IOnClickListener
{
public static readonly TextFieldClickHandler Instance = new TextFieldClickHandler();
diff --git a/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs
index b9114d22..e9cc3942 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs
@@ -2,19 +2,21 @@ using System;
using System.ComponentModel;
using System.Linq;
using Android.App;
+using Android.Content.Res;
using Android.Views;
using Android.Widget;
using ADatePicker = Android.Widget.DatePicker;
using ATimePicker = Android.Widget.TimePicker;
using Object = Java.Lang.Object;
+using Orientation = Android.Widget.Orientation;
namespace Xamarin.Forms.Platform.Android
{
public class PickerRenderer : ViewRenderer<Picker, EditText>
{
AlertDialog _dialog;
-
bool _isDisposed;
+ TextColorSwitcher _textColorSwitcher;
public PickerRenderer()
{
@@ -44,9 +46,11 @@ namespace Xamarin.Forms.Platform.Android
{
var textField = new EditText(Context) { Focusable = false, Clickable = true, Tag = this };
textField.SetOnClickListener(PickerListener.Instance);
+ _textColorSwitcher = new TextColorSwitcher(textField.TextColors);
SetNativeControl(textField);
}
UpdatePicker();
+ UpdateTextColor();
}
base.OnElementChanged(e);
@@ -60,6 +64,8 @@ namespace Xamarin.Forms.Platform.Android
UpdatePicker();
if (e.PropertyName == Picker.SelectedIndexProperty.PropertyName)
UpdatePicker();
+ if (e.PropertyName == Picker.TextColorProperty.PropertyName)
+ UpdateTextColor();
}
internal override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
@@ -148,6 +154,11 @@ namespace Xamarin.Forms.Platform.Android
((IVisualElementController)Element).NativeSizeChanged();
}
+ void UpdateTextColor()
+ {
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
+ }
+
class PickerListener : Object, IOnClickListener
{
public static readonly PickerListener Instance = new PickerListener();
diff --git a/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs
index cfa676d0..010eda72 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using Android.App;
+using Android.Content.Res;
using Android.Widget;
using ADatePicker = Android.Widget.DatePicker;
using ATimePicker = Android.Widget.TimePicker;
@@ -11,6 +12,7 @@ namespace Xamarin.Forms.Platform.Android
public class TimePickerRenderer : ViewRenderer<TimePicker, EditText>, TimePickerDialog.IOnTimeSetListener
{
AlertDialog _dialog;
+ TextColorSwitcher _textColorSwitcher;
public TimePickerRenderer()
{
@@ -36,17 +38,23 @@ namespace Xamarin.Forms.Platform.Android
textField.SetOnClickListener(TimePickerListener.Instance);
SetNativeControl(textField);
+ _textColorSwitcher = new TextColorSwitcher(textField.TextColors);
}
SetTime(e.NewElement.Time);
+ UpdateTextColor();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
- if (e.PropertyName == TimePicker.TimeProperty.PropertyName || e.PropertyName == TimePicker.FormatProperty.PropertyName)
+ if (e.PropertyName == TimePicker.TimeProperty.PropertyName ||
+ e.PropertyName == TimePicker.FormatProperty.PropertyName)
SetTime(Element.Time);
+
+ if (e.PropertyName == TimePicker.TextColorProperty.PropertyName)
+ UpdateTextColor();
}
internal override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
@@ -78,6 +86,11 @@ namespace Xamarin.Forms.Platform.Android
Control.Text = DateTime.Today.Add(time).ToString(Element.Format);
}
+ void UpdateTextColor()
+ {
+ _textColorSwitcher?.UpdateTextColor(Control, Element.TextColor);
+ }
+
class TimePickerListener : Object, IOnClickListener
{
public static readonly TimePickerListener Instance = new TimePickerListener();
diff --git a/Xamarin.Forms.Platform.Android/TextColorSwitcher.cs b/Xamarin.Forms.Platform.Android/TextColorSwitcher.cs
new file mode 100644
index 00000000..6beafd56
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/TextColorSwitcher.cs
@@ -0,0 +1,40 @@
+using Android.Content.Res;
+using Android.Widget;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ /// <summary>
+ /// Handles color state management for the TextColor property
+ /// for Button, Picker, TimePicker, and DatePicker
+ /// </summary>
+ internal class TextColorSwitcher
+ {
+ static readonly int[][] s_colorStates = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } };
+
+ readonly ColorStateList _defaultTextColors;
+ Color _currentTextColor;
+
+ public TextColorSwitcher(ColorStateList textColors)
+ {
+ _defaultTextColors = textColors;
+ }
+
+ public void UpdateTextColor(TextView control, Color color)
+ {
+ if (color == _currentTextColor)
+ return;
+
+ _currentTextColor = color;
+
+ if (color.IsDefault)
+ control.SetTextColor(_defaultTextColors);
+ else
+ {
+ // Set the new enabled state color, preserving the default disabled state color
+ int defaultDisabledColor = _defaultTextColors.GetColorForState(s_colorStates[1], color.ToAndroid());
+ control.SetTextColor(new ColorStateList(s_colorStates, new[] { color.ToAndroid().ToArgb(), defaultDisabledColor }));
+ }
+ }
+
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
index a6d4746f..b33427b9 100644
--- a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
+++ b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
@@ -28,7 +28,8 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
- <NoWarn>0642;0114;0108;0672;0168;0169;0184;0649;0414</NoWarn>
+ <NoWarn>
+ </NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -38,7 +39,8 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
- <NoWarn>0642;0114;0108;0672;0168;0169;0184;0649;0414</NoWarn>
+ <NoWarn>
+ </NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Turkey|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
@@ -168,6 +170,7 @@
<Compile Include="Renderers\ViewGroupExtensions.cs" />
<Compile Include="TapGestureHandler.cs" />
<Compile Include="TextViewExtensions.cs" />
+ <Compile Include="TextColorSwitcher.cs" />
<Compile Include="ViewInitializedEventArgs.cs" />
<Compile Include="VisualElementChangedEventArgs.cs" />
<Compile Include="RendererPool.cs" />