Custom Renderer to Button Border in Xamarin Forms

Also available in portuguese

If you program on ASP.NET Web Forms or even pure HTML you know when you need to change button border is very very very easy. BUT, in Xamarin Forms it’s another world. It’s become simple when you already know the solution: Custom Renderer.

I’m using Xamarin Forms “portable” and XAML frontend with Android API 23.

The XAML code below does NOT WORK on Android.

<Button Text="Click Me"
TextColor="Red"
BackgroundColor="White"
BorderColor="Red"
BorderWidth="1"
BorderRadius="3"
VerticalOptions="Center"
HorizontalOptions="Center">
</Button>

To work with Button Border and Radius for Android you need to create a Custom Renderer class on Droid project:

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;

[assembly: ExportRenderer(typeof(Button), typeof(FSL.XF1.Droid.FlatButtonRenderer))]
namespace FSL.XF1.Droid
{
    public class FlatButtonRenderer : ButtonRenderer
    {
        protected override void OnDraw(Android.Graphics.Canvas canvas)
        {
            base.OnDraw(canvas);
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
        }
    }
}

With custom implementation:
Custom Renderer to Button Border in Xamarin Forms

With NO custom implementation:
Custom Renderer to Button Border in Xamarin Forms

It´s simple right? The code tells for Xamarin Forms to render all buttons using Android behavior and not Xamarin Forms itself. All button properties came from XAML Xamarin Forms to ButtonRenderer. So in that class you can customize a lot of things.

Doesn’t need to create that class for iOS. iOS handles that XAML with no problems.

So, that’s it! Simple? Let’s face another problem… 🙂

This is it. I hope it helped.

Custom Renderer to Button Border in Xamarin Forms

Custom Renderer Xamarin Forms: Questions, suggestions and feedbacks will be appreciated. Good luck!

Do complete download of the source code on github.
About the Author:
He works as a solution architect and developer, has more than 16 years of experience in software development on several platforms and more than 14 years only for the insurance market.
  • hw

    Greate!!!! Thank you !!!!

  • Altevir C. Neto ViCo

    Showw!!!!!

  • Kaio Romero

    Very Nice !!!!! Thank You !!!!!

  • Daniel Pazos

    Thank you! Just what I was looking for.

  • Luis gerardo

    WORKS AMAZING NOW IN DECEMBER 25, 2017, Thank you very much!