using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.Content.PM; using Android.Content.Res; using Android.Graphics; using Android.Graphics.Drawables; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using Xamarin.Forms.Xaml; [assembly: ExportRenderer(typeof(GoiXe.CustomControls.Common.Button2), typeof(GoiXe.Droid.CustomRenderers.Common.Button2Renderer))] namespace GoiXe.Droid.CustomRenderers.Common { class Button2Renderer : Xamarin.Forms.Platform.Android.ButtonRenderer { private Android.Widget.Button androidButton; private GoiXe.CustomControls.Common.Button2 myButton; #pragma warning disable 618 public Button2Renderer() : base() #pragma warning restore 618 { } public Button2Renderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); if (Control != null) { androidButton = Control as Android.Widget.Button; } else { androidButton = CreateNativeControl(); SetNativeControl(androidButton); } if (e.OldElement != null) myButton = e.OldElement as GoiXe.CustomControls.Common.Button2; if (e.NewElement != null) myButton = e.NewElement as GoiXe.CustomControls.Common.Button2; if (Element != null) myButton = Element as GoiXe.CustomControls.Common.Button2; androidButton.LayoutParameters.Width = LayoutParams.WrapContent; androidButton.SetAllCaps(myButton.IsTextUppercase); androidButton.SetPadding((int)myButton.Padding.Left, (int)myButton.Padding.Top, (int)myButton.Padding.Right, (int)myButton.Padding.Bottom); androidButton.CompoundDrawablePadding = 2; //androidButton.SetPaddingRelative(0, 0, 0, 0); switch (myButton.VerticalTextAlignment) { case CustomControls.Common.Button2.TextAlignment.Center: androidButton.Gravity = GravityFlags.CenterVertical; break; case CustomControls.Common.Button2.TextAlignment.Start: androidButton.Gravity = GravityFlags.Top; break; case CustomControls.Common.Button2.TextAlignment.End: androidButton.Gravity = GravityFlags.Bottom; break; default: break; } switch (myButton.HorizontalTextAlignment) { case CustomControls.Common.Button2.TextAlignment.Center: androidButton.Gravity |= GravityFlags.CenterHorizontal; break; case CustomControls.Common.Button2.TextAlignment.Start: androidButton.Gravity |= GravityFlags.Left; break; case CustomControls.Common.Button2.TextAlignment.End: androidButton.Gravity |= GravityFlags.Right; break; default: break; } androidButton.TextAlignment = Android.Views.TextAlignment.Gravity; if (myButton.BgResourceId > 0) androidButton.SetBackgroundResource(myButton.BgResourceId); else androidButton.SetBackgroundResource(Resource.Drawable.story_poster_button_states); Drawable drawableLeft = null, drawableRight = null, drawableTop = null, drawableBottom = null; #pragma warning disable CS0618 // Type or member is obsolete if (myButton.ImageLeft?.Length > 0) { /* int resource = Resources.GetIdentifier(myButton.ImageLeft, "drawable", Context.PackageName); drawableLeft = Resources.GetDrawable(resource); */ string resourceName = myButton.ImageLeft.ToLower().Remove(myButton.ImageLeft.Length - 4); int id = (int) typeof(Resource.Drawable).GetField(resourceName).GetValue(null); drawableLeft = Resources.GetDrawable(id); } if (myButton.ImageRight?.Length > 0) { //int resource = Resources.GetIdentifier(myButton.ImageRight, "drawable", Context.PackageName); //drawableRight = Resources.GetDrawable(resource); string resourceName = myButton.ImageRight.ToLower().Remove(myButton.ImageRight.Length - 4); int id = (int)typeof(Resource.Drawable).GetField(resourceName).GetValue(null); drawableRight = Resources.GetDrawable(id); } if (myButton.ImageTop?.Length > 0) { //int resource = Resources.GetIdentifier(myButton.ImageTop, "drawable", Context.PackageName); //drawableTop = Resources.GetDrawable(resource); string resourceName = myButton.ImageTop.ToLower().Remove(myButton.ImageTop.Length - 4); int id = (int)typeof(Resource.Drawable).GetField(resourceName).GetValue(null); drawableTop = Resources.GetDrawable(id); } if (myButton.ImageBottom?.Length > 0) { //int resource = Resources.GetIdentifier(myButton.ImageBottom, "drawable", Context.PackageName); //drawableBottom = Resources.GetDrawable(resource); string resourceName = myButton.ImageBottom.ToLower().Remove(myButton.ImageBottom.Length - 4); int id = (int)typeof(Resource.Drawable).GetField(resourceName).GetValue(null); drawableBottom = Resources.GetDrawable(id); } #pragma warning restore CS0618 // Type or member is obsolete androidButton.SetCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom); //androidButton.StateListAnimator = null; } } }