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 GlobalResource = Android.Resource; using Object = Java.Lang.Object; using static System.String; namespace Xamarin.Forms.Platform.Android.AppCompat { public class ButtonRenderer : ViewRenderer, global::Android.Views.View.IOnAttachStateChangeListener { TextColorSwitcher _textColorSwitcher; float _defaultFontSize; Typeface _defaultTypeface; bool _isDisposed; int _imageHeight = -1; public ButtonRenderer() { AutoPackage = false; } global::Android.Widget.Button NativeButton => Control; void IOnAttachStateChangeListener.OnViewAttachedToWindow(global::Android.Views.View attachedView) { UpdateText(); } void IOnAttachStateChangeListener.OnViewDetachedFromWindow(global::Android.Views.View 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 AppCompatButton CreateNativeControl() { return new AppCompatButton(Context); } protected override void Dispose(bool disposing) { if (_isDisposed) return; _isDisposed = true; if (disposing) { if (Control != null) { Control.SetOnClickListener(null); Control.RemoveOnAttachStateChangeListener(this); Control.Tag = null; _textColorSwitcher = null; } } base.Dispose(disposing); } protected override void OnElementChanged(ElementChangedEventArgs