summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android
diff options
context:
space:
mode:
authorSamantha Houts <samhouts@users.noreply.github.com>2017-10-10 01:05:08 (GMT)
committerJason Smith <jason.smith@xamarin.com>2017-10-10 01:05:08 (GMT)
commit3035b128027e6316ce0193574389832b880091d8 (patch)
treebd5f110a2be6afe388ca3c75899fa1c41e93be59 /Xamarin.Forms.Platform.Android
parent68801433df0779e59f2f0c64ca0374593e94415b (diff)
downloadxamarin-forms-3035b128027e6316ce0193574389832b880091d8.zip
xamarin-forms-3035b128027e6316ce0193574389832b880091d8.tar.gz
xamarin-forms-3035b128027e6316ce0193574389832b880091d8.tar.bz2
Revert "Fix border on android buttons (#941)" (#1192)
This reverts commit 20d33c6ce21619b565a90af2ec4ca921581ff2b9.
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r--Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs60
-rw-r--r--Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs126
-rw-r--r--Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs78
-rw-r--r--Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs1
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs3
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs70
-rw-r--r--Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj1
7 files changed, 159 insertions, 180 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
index 9f336a2..394f332 100644
--- a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
@@ -1,9 +1,14 @@
using System;
using System.ComponentModel;
using Android.Content;
+using Android.Content.Res;
using Android.Graphics;
+using Android.Graphics.Drawables;
+using Android.Support.V4.Content;
using Android.Support.V7.Widget;
using Android.Util;
+using Xamarin.Forms.Internals;
+using GlobalResource = Android.Resource;
using Object = Java.Lang.Object;
using AView = Android.Views.View;
using AMotionEvent = Android.Views.MotionEvent;
@@ -14,7 +19,6 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
public class ButtonRenderer : ViewRenderer<Button, AppCompatButton>, AView.IOnAttachStateChangeListener
{
- ButtonBackgroundTracker _backgroundTracker;
TextColorSwitcher _textColorSwitcher;
float _defaultFontSize;
Typeface _defaultTypeface;
@@ -79,7 +83,6 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
Control.Tag = null;
_textColorSwitcher = null;
}
- _backgroundTracker?.Dispose();
}
base.Dispose(disposing);
@@ -108,12 +111,8 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
button.AddOnAttachStateChangeListener(this);
}
- if (_backgroundTracker == null)
- _backgroundTracker = new ButtonBackgroundTracker(Element, Control);
- else
- _backgroundTracker.Button = e.NewElement;
-
UpdateAll();
+ UpdateBackgroundColor();
}
}
@@ -140,7 +139,42 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
if (Element == null || Control == null)
return;
- _backgroundTracker?.UpdateBackgroundColor();
+ Color backgroundColor = Element.BackgroundColor;
+ if (backgroundColor.IsDefault)
+ {
+ if (Control.SupportBackgroundTintList != null)
+ {
+ Context context = Context;
+ int id = GlobalResource.Attribute.ButtonTint;
+ unchecked
+ {
+ using (var value = new TypedValue())
+ {
+ try
+ {
+ Resources.Theme theme = context.Theme;
+ if (theme != null && theme.ResolveAttribute(id, value, true))
+#pragma warning disable 618
+ Control.SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
+#pragma warning restore 618
+ else
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ }
+ catch (Exception ex)
+ {
+ Internals.Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer", "Could not retrieve button background resource: {0}", ex);
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ int intColor = backgroundColor.ToAndroid().ToArgb();
+ int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
+ Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
+ }
}
void UpdateAll()
@@ -150,16 +184,6 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
UpdateBitmap();
UpdateTextColor();
UpdateEnabled();
- UpdateBackgroundColor();
- UpdateDrawable();
- }
-
- void UpdateDrawable()
- {
- if (Element == null || Control == null)
- return;
-
- _backgroundTracker?.UpdateDrawable();
}
void UpdateBitmap()
diff --git a/Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs b/Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs
deleted file mode 100644
index 33c5074..0000000
--- a/Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs
+++ /dev/null
@@ -1,126 +0,0 @@
-´╗┐using System;
-using System.ComponentModel;
-using Android.Graphics.Drawables;
-using AButton = Android.Widget.Button;
-
-namespace Xamarin.Forms.Platform.Android
-{
- internal class ButtonBackgroundTracker : IDisposable
- {
- Drawable _defaultDrawable;
- ButtonDrawable _backgroundDrawable;
- Button _button;
- AButton _nativeButton;
- bool _drawableEnabled;
- bool _disposed;
-
- public ButtonBackgroundTracker(Button button, AButton nativeButton)
- {
- Button = button;
- _nativeButton = nativeButton;
- }
-
- public Button Button
- {
- get { return _button; }
- set
- {
- if (_button == value)
- return;
- if (_button != null)
- _button.PropertyChanged -= ButtonPropertyChanged;
- _button = value;
- _button.PropertyChanged += ButtonPropertyChanged;
- }
- }
-
- public void UpdateDrawable()
- {
- if (_button == null || _nativeButton == null)
- return;
-
- if (_button.BackgroundColor == Color.Default)
- {
- if (!_drawableEnabled)
- return;
-
- if (_defaultDrawable != null)
- _nativeButton.SetBackground(_defaultDrawable);
-
- _drawableEnabled = false;
- }
- else
- {
- if (_backgroundDrawable == null)
- _backgroundDrawable = new ButtonDrawable();
-
- _backgroundDrawable.Button = _button;
-
- if (_drawableEnabled)
- return;
-
- if (_defaultDrawable == null)
- _defaultDrawable = _nativeButton.Background;
-
- _nativeButton.SetBackground(_backgroundDrawable);
- _drawableEnabled = true;
- }
-
- _nativeButton.Invalidate();
- }
-
- public void Reset()
- {
- if (_drawableEnabled)
- {
- _drawableEnabled = false;
- _backgroundDrawable?.Reset();
- _backgroundDrawable = null;
- }
- }
-
- public void UpdateBackgroundColor()
- {
- UpdateDrawable();
- }
-
- public void Dispose()
- {
- Dispose(true);
- }
-
- protected virtual void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- if (disposing)
- {
- _backgroundDrawable?.Dispose();
- _backgroundDrawable = null;
- _defaultDrawable?.Dispose();
- _defaultDrawable = null;
- if (_button != null)
- {
- _button.PropertyChanged -= ButtonPropertyChanged;
- _button = null;
- }
- _nativeButton = null;
- }
- _disposed = true;
- }
- }
-
- void ButtonPropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if (e.PropertyName.Equals(Button.BorderColorProperty.PropertyName) ||
- e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName) ||
- e.PropertyName.Equals(Button.BorderRadiusProperty.PropertyName) ||
- e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName))
- {
- Reset();
- UpdateDrawable();
- }
- }
-
- }
-} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs
index cce7578..898c85d 100644
--- a/Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs
@@ -1,15 +1,19 @@
using System;
using System.ComponentModel;
using Android.Content;
+using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Xamarin.Forms.Internals;
+using GlobalResource = Android.Resource;
using AView = Android.Views.View;
+using AMotionEvent = Android.Views.MotionEvent;
using AMotionEventActions = Android.Views.MotionEventActions;
using static System.String;
+using Object = Java.Lang.Object;
namespace Xamarin.Forms.Platform.Android.FastRenderers
{
@@ -26,7 +30,6 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
readonly AutomationPropertiesProvider _automationPropertiesProvider;
readonly EffectControlProvider _effectControlProvider;
VisualElementTracker _tracker;
- ButtonBackgroundTracker _backgroundTracker;
public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;
@@ -121,11 +124,6 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
oldElement.PropertyChanged -= OnElementPropertyChanged;
}
- if (_backgroundTracker == null)
- _backgroundTracker = new ButtonBackgroundTracker(Button, this);
- else
- _backgroundTracker.Button = Button;
-
Color currentColor = oldElement?.BackgroundColor ?? Color.Default;
if (element.BackgroundColor != currentColor)
{
@@ -184,8 +182,6 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
_automationPropertiesProvider?.Dispose();
_tracker?.Dispose();
- _backgroundTracker?.Dispose();
-
if (Element != null)
{
Element.PropertyChanged -= OnElementPropertyChanged;
@@ -213,11 +209,7 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
void OnElementChanged(ElementChangedEventArgs<Button> e)
{
- if (e.OldElement != null)
- {
- _backgroundTracker?.Reset();
- }
- if (e.NewElement != null && !_isDisposed)
+ if (e.NewElement != null)
{
this.EnsureId();
@@ -228,7 +220,6 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
UpdateIsEnabled();
UpdateInputTransparent();
UpdateBackgroundColor();
- UpdateDrawable();
}
ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(e.OldElement, e.NewElement));
@@ -263,6 +254,10 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
else if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName)
{
UpdateInputTransparent();
+ }
+ else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
+ {
+ UpdateBackgroundColor();
}
ElementPropertyChanged?.Invoke(this, e);
@@ -294,7 +289,54 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
void UpdateBackgroundColor()
{
- _backgroundTracker?.UpdateBackgroundColor();
+ if (Element == null)
+ {
+ return;
+ }
+
+ Color backgroundColor = Element.BackgroundColor;
+ if (backgroundColor.IsDefault)
+ {
+ if (SupportBackgroundTintList != null)
+ {
+ Context context = Context;
+ int id = GlobalResource.Attribute.ButtonTint;
+ unchecked
+ {
+ using (var value = new TypedValue())
+ {
+ try
+ {
+ Resources.Theme theme = context.Theme;
+ if (theme != null && theme.ResolveAttribute(id, value, true))
+#pragma warning disable 618
+ {
+ SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
+ }
+#pragma warning restore 618
+ else
+ {
+ SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
+ new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ }
+ }
+ catch (Exception ex)
+ {
+ Internals.Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer",
+ "Could not retrieve button background resource: {0}", ex);
+ SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
+ new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ int intColor = backgroundColor.ToAndroid().ToArgb();
+ int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
+ SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
+ }
}
internal void OnNativeFocusChanged(bool hasFocus)
@@ -456,11 +498,5 @@ namespace Xamarin.Forms.Platform.Android.FastRenderers
_textColorSwitcher.Value.UpdateTextColor(this, Button.TextColor);
}
-
- void UpdateDrawable()
- {
- _backgroundTracker?.UpdateDrawable();
- }
-
}
}
diff --git a/Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs b/Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
index 9fee726..2b04610 100644
--- a/Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
+++ b/Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
@@ -38,6 +38,7 @@ using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer (typeof (Stepper), typeof (StepperRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]
[assembly: ExportRenderer (typeof (ScrollView), typeof (ScrollViewRenderer))]
+[assembly: ExportRenderer (typeof (Toolbar), typeof (ToolbarRenderer))]
[assembly: ExportRenderer (typeof (ActivityIndicator), typeof (ActivityIndicatorRenderer))]
[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))]
[assembly: ExportRenderer (typeof (NavigationMenu), typeof (NavigationMenuRenderer))]
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs b/Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs
index 5852f47..62eaecb 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs
@@ -1,7 +1,6 @@
using System.Linq;
using Android.Graphics;
using Android.Graphics.Drawables;
-using System;
namespace Xamarin.Forms.Platform.Android
{
@@ -137,7 +136,7 @@ namespace Xamarin.Forms.Platform.Android
float inset = borderWidth / 2;
// adjust border radius so outer edge of stroke is same radius as border radius of background
- float borderRadius = Math.Max(Forms.Context.ToPixels(Button.BorderRadius) - inset, 0);
+ float borderRadius = Forms.Context.ToPixels(Button.BorderRadius) - inset;
path.AddRoundRect(new RectF(inset, inset, width - inset, height - inset), borderRadius, borderRadius, Path.Direction.Cw);
paint.StrokeWidth = borderWidth;
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
index 0aac4ed..e07a2be 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
@@ -1,6 +1,8 @@
using System;
using System.ComponentModel;
+using Android.Content.Res;
using Android.Graphics;
+using Android.Graphics.Drawables;
using Android.Util;
using static System.String;
using AButton = Android.Widget.Button;
@@ -13,10 +15,12 @@ namespace Xamarin.Forms.Platform.Android
{
public class ButtonRenderer : ViewRenderer<Button, AButton>, AView.IOnAttachStateChangeListener
{
- ButtonBackgroundTracker _backgroundTracker;
+ ButtonDrawable _backgroundDrawable;
TextColorSwitcher _textColorSwitcher;
+ Drawable _defaultDrawable;
float _defaultFontSize;
Typeface _defaultTypeface;
+ bool _drawableEnabled;
bool _isDisposed;
int _imageHeight = -1;
@@ -68,7 +72,11 @@ namespace Xamarin.Forms.Platform.Android
if (disposing)
{
- _backgroundTracker?.Dispose();
+ if (_backgroundDrawable != null)
+ {
+ _backgroundDrawable.Dispose();
+ _backgroundDrawable = null;
+ }
}
base.Dispose(disposing);
@@ -97,11 +105,15 @@ namespace Xamarin.Forms.Platform.Android
button.AddOnAttachStateChangeListener(this);
}
}
-
- if (_backgroundTracker == null)
- _backgroundTracker = new ButtonBackgroundTracker(Element, Control);
else
- _backgroundTracker.Button = e.NewElement;
+ {
+ if (_drawableEnabled)
+ {
+ _drawableEnabled = false;
+ _backgroundDrawable.Reset();
+ _backgroundDrawable = null;
+ }
+ }
UpdateAll();
}
@@ -116,20 +128,27 @@ namespace Xamarin.Forms.Platform.Android
UpdateEnabled();
else if (e.PropertyName == Button.FontProperty.PropertyName)
UpdateFont();
+ else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
+ UpdateDrawable();
else if (e.PropertyName == Button.ImageProperty.PropertyName)
UpdateBitmap();
else if (e.PropertyName == VisualElement.IsVisibleProperty.PropertyName)
UpdateText();
-
+
+ if (_drawableEnabled &&
+ (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == Button.BorderColorProperty.PropertyName || e.PropertyName == Button.BorderRadiusProperty.PropertyName ||
+ e.PropertyName == Button.BorderWidthProperty.PropertyName))
+ {
+ _backgroundDrawable.Reset();
+ Control.Invalidate();
+ }
+
base.OnElementPropertyChanged(sender, e);
}
protected override void UpdateBackgroundColor()
{
- if (Element == null || Control == null)
- return;
-
- _backgroundTracker?.UpdateBackgroundColor();
+ // Do nothing, the drawable handles this now
}
void UpdateAll()
@@ -200,7 +219,34 @@ namespace Xamarin.Forms.Platform.Android
void UpdateDrawable()
{
- _backgroundTracker.UpdateDrawable();
+ if (Element.BackgroundColor == Color.Default)
+ {
+ if (!_drawableEnabled)
+ return;
+
+ if (_defaultDrawable != null)
+ Control.SetBackground(_defaultDrawable);
+
+ _drawableEnabled = false;
+ }
+ else
+ {
+ if (_backgroundDrawable == null)
+ _backgroundDrawable = new ButtonDrawable();
+
+ _backgroundDrawable.Button = Element;
+
+ if (_drawableEnabled)
+ return;
+
+ if (_defaultDrawable == null)
+ _defaultDrawable = Control.Background;
+
+ Control.SetBackground(_backgroundDrawable);
+ _drawableEnabled = true;
+ }
+
+ Control.Invalidate();
}
void UpdateEnabled()
diff --git a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
index 8539ddc..aa91ded 100644
--- a/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
+++ b/Xamarin.Forms.Platform.Android/Xamarin.Forms.Platform.Android.csproj
@@ -102,7 +102,6 @@
<Compile Include="AndroidApplicationLifecycleState.cs" />
<Compile Include="AndroidTitleBarVisibility.cs" />
<Compile Include="AppCompat\FrameRenderer.cs" />
- <Compile Include="ButtonBackgroundTracker.cs" />
<Compile Include="Extensions\JavaObjectExtensions.cs" />
<Compile Include="FastRenderers\AutomationPropertiesProvider.cs" />
<Compile Include="FastRenderers\ButtonRenderer.cs" />