Skip to content

Instantly share code, notes, and snippets.

@gitbash-basher
Last active April 19, 2019 09:19
Show Gist options
  • Save gitbash-basher/8ec4220131743d7d7eefcebee9e3890c to your computer and use it in GitHub Desktop.
Save gitbash-basher/8ec4220131743d7d7eefcebee9e3890c to your computer and use it in GitHub Desktop.
using Android.Content;
using Android.Graphics;
using Android.Views;
using SampleCode.Custom;
using SampleCode.Droid.Custom;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace SampleCode.Droid.Custom
{
public class CustomButtonRenderer : ButtonRenderer
{
Rect rect;
public CustomButtonRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
//Flat design button
//Set the button state color on your button_background_style.xml
Control.SetBackgroundResource(Resource.Drawable.button_background_style);
Control.StateListAnimator = null;
Control.Elevation = 0;
}
}
public override bool OnInterceptTouchEvent(MotionEvent e)
{
if (e.ActionMasked == MotionEventActions.Down)
{
Control.SetTextColor(Android.Graphics.Color.ParseColor("#2679F9"));
}
else if (e.ActionMasked == MotionEventActions.Up || e.ActionMasked == MotionEventActions.Cancel)
{
Control.SetTextColor(Android.Graphics.Color.White);
}
else if (e.ActionMasked == MotionEventActions.Move)
{
//Change to default textcolor if the user tap, hold and drag on button
rect = new Rect(Left, Top, Right, Bottom);
if (!rect.Contains(Left + (int)e.GetX(), Top + (int)e.GetY()))
{
Control.SetTextColor(Android.Graphics.Color.White);
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment