protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) { base.OnElementChanged(e); if (e.OldElement == null) { androidButton = (global::Android.Widget.Button)Control; formsElement = (CustomImageButton)this.Element; androidButton.SetPadding(20, 20, 20, 20); androidButton.SetAllCaps(false); if (formsElement.ImageName != null) androidButton.SetBackgroundDrawable(Context.Resources.GetDrawable(formsElement.ImageName)); if (formsElement.TextOrientation != null) { if (Convert.ToString(formsElement.TextOrientation) == "Left") { androidButton.Gravity = Android.Views.GravityFlags.Left; } else if (Convert.ToString(formsElement.TextOrientation) == "Right") { androidButton.Gravity = Android.Views.GravityFlags.Right; } else { androidButton.Gravity = Android.Views.GravityFlags.Center; } } } }
public float ButtonTextSize(string text, double fontSize) { if (_button == null) { _button = new global::Android.Widget.Button(Forms.Context); // _button.SetPadding(10, _button.PaddingTop, 10, _button.PaddingBottom); _button.SetPadding(0, _button.PaddingTop, 0, _button.PaddingBottom); } if (fontSize != 0) { _button.TextSize = (float)fontSize; } var widgetPadding = 8; var bounds = new Rect(); _button.Text = text; _button.Paint.GetTextBounds(text, 0, text.Length, bounds); // Add two times the widgetPadding of 8 dp, because the button has this min size and than resize to the next multi of 8. // http://developer.android.com/guide/practices/ui_guidelines/widget_design.html return (float)Math.Ceiling((bounds.Width() / Resources.System.DisplayMetrics.Density + _button.PaddingLeft + _button.PaddingRight + 2 * widgetPadding) / 8) * 8; }