Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Border Radius AppCompat

It seems that the BorderRadius attribute is not working when including AppCompat in the project.

I tried to create a custom render like this discussed here, but it didn't work:

namespace Xamarin.Forms
{
    public class CustomButton : Button  
    {
        public CustomButton():base()
        {   
        }

        protected override void OnParentSet()
        {
            base.OnParentSet();
        }
    }
}

In the Android project:

[assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
namespace CalculateurMadelin.Droid.Renderers
{
        public class CustomButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
    { }
}
like image 937
Renan Ferreira Avatar asked May 09 '16 22:05

Renan Ferreira


Video Answer


1 Answers

You can load an Android drawable in your custom renderer to define the background on your AppCompat.Button:


[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 matches the name you are using in your SetBackgroundResource (i.e.. CustomButtonBackground.axml), in this I am setting a corner radius 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>

enter image description here

like image 76
SushiHangover Avatar answered Oct 16 '22 06:10

SushiHangover