summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-06-16 10:35:24 -0600
committerRui Marinho <me@ruimarinho.net>2016-06-16 17:35:24 +0100
commit9df4d9c3285952d8ff3ff7940457f44f35a34f81 (patch)
tree23d83d152c0854b8462acfc6107430e602f2a49e /Xamarin.Forms.Platform.Android
parent8dd51f2b49799bf7c0e51efe1d339dec44d91220 (diff)
downloadxamarin-forms-9df4d9c3285952d8ff3ff7940457f44f35a34f81.tar.gz
xamarin-forms-9df4d9c3285952d8ff3ff7940457f44f35a34f81.tar.bz2
xamarin-forms-9df4d9c3285952d8ff3ff7940457f44f35a34f81.zip
Add repro of uncentered button image for Android (#176)
Fix vertical centering logic for button images in Android
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r--Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs38
-rw-r--r--Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs41
2 files changed, 69 insertions, 10 deletions
diff --git a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
index 8f4ef35e..2d175860 100644
--- a/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
@@ -9,6 +9,7 @@ using Android.Support.V7.Widget;
using Android.Util;
using GlobalResource = Android.Resource;
using Object = Java.Lang.Object;
+using static System.String;
namespace Xamarin.Forms.Platform.Android.AppCompat
{
@@ -18,6 +19,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
float _defaultFontSize;
Typeface _defaultTypeface;
bool _isDisposed;
+ int _imageHeight = -1;
public ButtonRenderer()
{
@@ -41,6 +43,20 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
return base.GetDesiredSize(widthConstraint, heightConstraint);
}
+ protected override void OnLayout(bool changed, int l, int t, int r, int b)
+ {
+ if (_imageHeight > -1)
+ {
+ // We've got an image (and no text); it's already centered horizontally,
+ // we just need to adjust the padding so it centers vertically
+ var diff = (b - t - _imageHeight) / 2;
+ diff = Math.Max(diff, 0);
+ Control?.SetPadding(0, diff, 0, -diff);
+ }
+
+ base.OnLayout(changed, l, t, r, b);
+ }
+
protected override AppCompatButton CreateNativeControl()
{
return new AppCompatButton(Context);
@@ -161,6 +177,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
var elementImage = Element.Image;
var imageFile = elementImage?.File;
+ _imageHeight = -1;
if (elementImage == null || string.IsNullOrEmpty(imageFile))
{
@@ -170,14 +187,20 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
var image = Context.Resources.GetDrawable(imageFile);
- if (string.IsNullOrEmpty(Element.Text))
+ if (IsNullOrEmpty(Element.Text))
{
// No text, so no need for relative position; just center the image
// There's no option for just plain-old centering, so we'll use Top
- // (which handles the horizontal centering) and some tricksy padding
- // to handle the vertical centering
+ // (which handles the horizontal centering) and some tricksy padding (in OnLayout)
+ // to handle the vertical centering
+
+ // Clear any previous padding and set the image as top/center
+ Control.SetPadding(0, 0, 0, 0);
Control.SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
- Control.SetPadding(0, Control.PaddingTop, 0, -Control.PaddingTop);
+
+ // Keep track of the image height so we can use it in OnLayout
+ _imageHeight = image.IntrinsicHeight;
+
image?.Dispose();
return;
}
@@ -239,7 +262,14 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
void UpdateText()
{
+ var oldText = NativeButton.Text;
NativeButton.Text = Element.Text;
+
+ // If we went from or to having no text, we need to update the image position
+ if (IsNullOrEmpty(oldText) != IsNullOrEmpty(NativeButton.Text))
+ {
+ UpdateBitmap();
+ }
}
void UpdateTextColor()
diff --git a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
index 7247361a..5de429af 100644
--- a/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Renderers/ButtonRenderer.cs
@@ -4,6 +4,7 @@ using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Util;
+using static System.String;
using AButton = Android.Widget.Button;
using AView = Android.Views.View;
using Object = Java.Lang.Object;
@@ -18,8 +19,8 @@ namespace Xamarin.Forms.Platform.Android
float _defaultFontSize;
Typeface _defaultTypeface;
bool _drawableEnabled;
-
bool _isDisposed;
+ int _imageHeight = -1;
public ButtonRenderer()
{
@@ -46,6 +47,20 @@ namespace Xamarin.Forms.Platform.Android
return base.GetDesiredSize(widthConstraint, heightConstraint);
}
+ protected override void OnLayout(bool changed, int l, int t, int r, int b)
+ {
+ if (_imageHeight > -1)
+ {
+ // We've got an image (and no text); it's already centered horizontally,
+ // we just need to adjust the padding so it centers vertically
+ var diff = (b - t - _imageHeight) / 2;
+ diff = Math.Max(diff, 0);
+ Control?.SetPadding(0, diff, 0, -diff);
+ }
+
+ base.OnLayout(changed, l, t, r, b);
+ }
+
protected override void Dispose(bool disposing)
{
if (_isDisposed)
@@ -142,8 +157,9 @@ namespace Xamarin.Forms.Platform.Android
{
var elementImage = Element.Image;
var imageFile = elementImage?.File;
+ _imageHeight = -1;
- if (elementImage == null || string.IsNullOrEmpty(imageFile))
+ if (elementImage == null || IsNullOrEmpty(imageFile))
{
Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
return;
@@ -151,14 +167,20 @@ namespace Xamarin.Forms.Platform.Android
var image = Context.Resources.GetDrawable(imageFile);
- if (string.IsNullOrEmpty(Element.Text))
+ if (IsNullOrEmpty(Element.Text))
{
// No text, so no need for relative position; just center the image
// There's no option for just plain-old centering, so we'll use Top
- // (which handles the horizontal centering) and some tricksy padding
- // to handle the vertical centering
+ // (which handles the horizontal centering) and some tricksy padding (in OnLayout)
+ // to handle the vertical centering
+
+ // Clear any previous padding and set the image as top/center
+ Control.SetPadding(0, 0, 0, 0);
Control.SetCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
- Control.SetPadding(0, Control.PaddingTop, 0, -Control.PaddingTop);
+
+ // Keep track of the image height so we can use it in OnLayout
+ _imageHeight = image.IntrinsicHeight;
+
image?.Dispose();
return;
}
@@ -250,7 +272,14 @@ namespace Xamarin.Forms.Platform.Android
void UpdateText()
{
+ var oldText = NativeButton.Text;
NativeButton.Text = Element.Text;
+
+ // If we went from or to having no text, we need to update the image position
+ if (IsNullOrEmpty(oldText) != IsNullOrEmpty(NativeButton.Text))
+ {
+ UpdateBitmap();
+ }
}
void UpdateTextColor()