Skip to content

Instantly share code, notes, and snippets.

@linhdh
Created December 19, 2017 06:47
Show Gist options
  • Save linhdh/6f30900af0c9ae1463f32bb21d6f11ac to your computer and use it in GitHub Desktop.
Save linhdh/6f30900af0c9ae1463f32bb21d6f11ac to your computer and use it in GitHub Desktop.
Circular Button Using Xamarin (Custom Renderer)
class CircularButtonRender : Xamarin.Forms.Platform.Android.ButtonRenderer
{
private GradientDrawable _NormalState, _PressedState;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var button = e.NewElement;
// Create a drawable for the button's normal state
_NormalState = new Android.Graphics.Drawables.GradientDrawable();
_NormalState.SetColor(button.BackgroundColor.ToAndroid());
_NormalState.SetStroke((int)button.BorderRadius, button.BorderColor.ToAndroid());
_NormalState.SetShape(ShapeType.Oval);
_PressedState = new Android.Graphics.Drawables.GradientDrawable();
_PressedState.SetColor(button.BackgroundColor.ToAndroid());
_PressedState.SetStroke((int)button.BorderRadius, button.BorderColor.ToAndroid());
_PressedState.SetShape(ShapeType.Oval);
// Add the drawables to a state list and assign the state list to the button
var sld = new StateListDrawable();
sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _PressedState);
sld.AddState(new int[] { }, _NormalState);
Control.SetBackground(sld);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment