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; using AView = Android.Views.View; using AMotionEvent = Android.Views.MotionEvent; using AMotionEventActions = Android.Views.MotionEventActions; using Object = Java.Lang.Object; namespace Xamarin.Forms.Platform.Android { public class ButtonRenderer : ViewRenderer, AView.IOnAttachStateChangeListener { ButtonDrawable _backgroundDrawable; TextColorSwitcher _textColorSwitcher; Drawable _defaultDrawable; float _defaultFontSize; Typeface _defaultTypeface; bool _drawableEnabled; bool _isDisposed; int _imageHeight = -1; public ButtonRenderer() { AutoPackage = false; } AButton NativeButton { get { return Control; } } public void OnViewAttachedToWindow(AView attachedView) { UpdateText(); } public void OnViewDetachedFromWindow(AView detachedView) { } public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint) { UpdateText(); 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) return; _isDisposed = true; if (disposing) { if (_backgroundDrawable != null) { _backgroundDrawable.Dispose(); _backgroundDrawable = null; } } base.Dispose(disposing); } protected override AButton CreateNativeControl() { return new AButton(Context); } protected override void OnElementChanged(ElementChangedEventArgs