Skip to content

Instantly share code, notes, and snippets.

@linhdh
Created December 22, 2017 08:36
Show Gist options
  • Save linhdh/7bb28231e518e5789b6e02cf09a8faa6 to your computer and use it in GitHub Desktop.
Save linhdh/7bb28231e518e5789b6e02cf09a8faa6 to your computer and use it in GitHub Desktop.
[Xamarin forms] A button with custom renderer for Android.
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace GoiXe.CustomControls.Common
{
public class Button2 : Xamarin.Forms.Button
{
public enum TextAlignment
{
Start,
End,
Center
}
public string ImageLeft { get; set; }
public string ImageRight { get; set; }
public string ImageTop { get; set; }
public string ImageBottom { get; set; }
public bool IsTextUppercase { get; set; } = false;
public TextAlignment VerticalTextAlignment { get; set; } = TextAlignment.Center;
public TextAlignment HorizontalTextAlignment { get; set; } = TextAlignment.Start;
public int BgResourceId { get; set; }
public Thickness Padding { get; set; } = new Thickness(0);
}
}
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<Xamarin.Forms.Button> 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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment