How to create a rounded button in Xamarin.Forms

Asked

Viewed 1,502 times

0

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

Edit1: I forgot to comment that I am using Formsappcompatactivity to enable material design on Android

1 answer

2

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

Following documentation of Xamarin:

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

In case you are having problems for the rounded button on Android da para você fazer a custom render.

[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 that you are using in your SetBackgroundResource for ex. CustomButtonBackground.axml, thus difinindo 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>

inserir a descrição da imagem aqui

Note: I did the translation of that Soeng response

  • I tried to use this property but it has no effect on my button. I tried various values, I took other properties to see if any could influence the other, but no.

  • not working only on Android, or at all?

  • Only on Android because of Formsappcompact, I saw this answer too, only how do I call this button in Xamarin.Forms? If you answer this I put as resolved. Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.