How to create a round button in Xamarin.Forms

0

I'm needing to create a rounded button in Xamarin.Forms similar to that of the main design material, but without losing the properties of a conventional button and without losing the feature of all xamarin.forms platforms. I would like to use the best practice to do this, without using gambiarras with images etc. Thanks guys!

Edit1: I forgot to comment that I'm using FormsAppCompatActivity to enable material design on Android

    
asked by anonymous 20.03.2017 / 19:54

1 answer

2

You can use the BorderRadius property to create a button with rounded corners:

Following the Xamarin documentation :

<Button Text="BlueButton"
        BorderColor="Blue"
        BorderRadius = "5"
        BorderWidth = "2"/>

If you're having problems with the round button on Android, you can do it a custom rendering.

[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace AppCompatRender.Droid
{
    public class CustomButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {
                Control.SetBackgroundResource(Resource.Drawable.CustomButtonBackground);
            }
        }
    }
}

Add a new Resources/Drawable that has the same name you're using in your SetBackgroundResource for ex. CustomButtonBackground.axml , thus defining the corners of the rectangle as 10dp :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="10dp" />
</shape>

Note:Ididthe translation of this SOEng response

    
20.03.2017 / 21:53