BorderRadius / Corner Radius with AppCompat using Forms on a Button

Anyone have notice that if you set your app to use FormAppCompartActivity, BorderRadius on Xamarin Forms seems to stop working? I've tried using BorderRadius with higher values (Example: 50) and nothing, the button is still using the same material default appearance.

Anyone has a workaround to achieve this without removing FormsAppCompatActivity on the Android side

Best Answer

Answers

  • AlmaJensen.9398AlmaJensen.9398 USMember ✭✭✭

    I haven't had luck using borderradius on my project either on Android. Works well on iOS though. I hadn't thought about it being appcompat related though.

  • CarlosCamposSVCarlosCamposSV SVMember

    I ended using the default appearance (AppCompat Material) for the moment. But I will look the way to make a custom render for this. Thank you @MichaelRumpler & @AlmaJensen.9398

  • Campos finally got the answer

    Create a drawable xml with your button style (include corner radius).

    <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="@color/primaryDark" />
    <corners android:radius="5dp" />
    <stroke android:width="0px" android:color="#000000" />
    </shape>
    </item>
    <item android:state_focused="true" >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/primaryDark" />
    <corners android:radius="5dp" />
    <stroke android:width="0px" android:color="#000000" />
    </shape>
    </item>
    <item >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/primary" />
    <corners android:radius="5dp" />
    <stroke android:width="0px" android:color="#000000" />
    </shape>
    </item>
    </selector>

    Your renderer should to inherit from Xamarin.Forms.Platform.Android.ButtonRenderer
    Then create a nativeControl, an instance from Android.Widget.Button and set your drawable as background, set Control properties too
    var nativeControl = new Android.Widget.Button(Context);
    nativeControl.SetBackground(Resources.GetDrawable(customButton.AndroidStyle));
    nativeControl.Text = customButton.Text;
    nativeControl.Typeface = Control.Typeface;
    nativeControl.SetTextColor(Xamarin.Forms.Color.White.ToAndroid());
    SetNativeControl(nativeControl);

    Now it should work :)

  • GregCadmesGregCadmes USUniversity ✭✭
    edited July 2017

    @RobertoErnestoJovelBarrera.44
    Question,
    Did you create the new Android.Widget.Button inside the Renderer's constructor, or the OnElementChanged event handler?

    Also, what is the control, 'customButton' in your example? Please explain.

    thank you

  • Oracle22Oracle22 USMember ✭✭
    edited September 2017

    I have resolve my problem at create renderer Button.
    J'hérite de class FormsAppCompatActivity in My MainActivity et j'ai créé un ButtonRender qui hérite de Xamarin.Forms.Platform.Android.ButtonRender et non de Xamarin.Forms.Platform.Android.AppCompact.
    J'ai donc récupéré mes radius

    using CHR.Droid.DependencyService.Renderer;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(MyButtonRenderer))]
    namespace CHR.Droid.DependencyService.Renderer
    {
        public class MyButtonRenderer : ButtonRenderer
        {
            public MyButtonRenderer()
            {
    
            }
        }
    }
    
  • Oracle22Oracle22 USMember ✭✭
    edited September 2017

    I have resolve my problem to create.
    I inherited Class FormsAppCompatActivity in My MainActivity and I created a ButtonRender that inherits from Xamarin.Forms.Platform.Android.ButtonRender and not from Xamarin.Forms.Platform.Android.AppCompact.
    So I recovered my radius

    using CHR.Droid.DependencyService.Renderer;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(MyButtonRenderer))]
    namespace CHR.Droid.DependencyService.Renderer
    {
        public class MyButtonRenderer : ButtonRenderer
        {
            public MyButtonRenderer()
            {
    
            }
        }
    }
    
Sign In or Register to comment.