Does anyone have a complete example of a Xamarin Forms android button custom renderer? Or is that even possible?
I am basically looking to make round corners button.
I tried starting from this example: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/entry/
Using http://taoffi.isosoft.org/post/2016/03/26/Xamarin-forms-so-you-lost-your-rounded-buttons
But I get loads of errors at compilation:
using System;
using System.Collections.Generic;
using System.Linq;
//using UXDivers.Artina.Shared;
using Xamarin.Forms;
using Android.Graphics.Drawables;
using System.ComponentModel;
using Xamarin.Forms.Platform.Android;
using Android.Graphics;
using Android.Views;
using System.Runtime.Remoting.Contexts;
using CustomRenderer.Android;
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonCompatRenderer))]
namespace CustomRenderer.Android
{
public class CustomButtonCompatRenderer : ButtonRenderer
{
public CustomButtonCompatRenderer(Context context) : base(context)
{
SetWillNotDraw(false);
}
private GradientDrawable _normal,
_pressed;
// resolves: button text alignment lost after click or IsEnabled change
//public override void ChildDrawableStateChanged(Android.Views.View child)
//{
// base.ChildDrawableStateChanged(child);
// Control.Text = Control.Text;
//}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
SetAlignment();
var density = Math.Max(1, Resources.DisplayMetrics.Density);
var button = e.NewElement;
var mode = MeasureSpec.GetMode((int)button.BorderRadius);
var borderRadius = button.BorderRadius * density;
var borderWidth = button.BorderWidth * density;
// Create a drawable for the button's normal state
_normal = new Android.Graphics.Drawables.GradientDrawable();
if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0)
_normal.SetColor(Android.Graphics.Color.ParseColor("#ff2c2e2f"));
else
_normal.SetColor(button.BackgroundColor.ToAndroid());
_normal.SetStroke((int)borderWidth, button.BorderColor.ToAndroid());
_normal.SetCornerRadius(borderRadius);
// Create a drawable for the button's pressed state
_pressed = new Android.Graphics.Drawables.GradientDrawable();
var highlight = Context.ObtainStyledAttributes(new int[]
{
Android.Resource.Attribute.ColorAccent // .ColorActivatedHighlight
}).GetColor(0, Android.Graphics.Color.Gray);
_pressed.SetColor(highlight);
_pressed.SetStroke((int)borderWidth, button.BorderColor.ToAndroid());
_pressed.SetCornerRadius(borderRadius);
// Add the drawables to a state list and assign the state list to the button
var sld = new StateListDrawable();
sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed);
sld.AddState(new int[] { }, _normal);
Control.SetBackground(sld); //.SetBackgroundDrawable(sld); // deprecated
}
}
private void SetAlignment()
{
var element = this.Element as Button;
if (element == null || this.Control == null)
{
return;
}
this.Control.Gravity = GravityFlags.CenterHorizontal | GravityFlags.CenterVertical;
//element.VerticalAlignment.ToDroidVerticalGravity() |
//element.HorizontalAlignment.ToDroidHorizontalGravity();
}
void DrawCustom(Button targetButton)
{
if (Control == null || targetButton == null)
return;
}
}
}
Errors:
CustomButtonCompatRenderer.cs(20,62,20,66): error CS1729: 'ButtonRenderer' does not contain a constructor that takes 1 arguments
CustomButtonCompatRenderer.cs(52,39,52,47): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(55,38,55,54): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(63,40,63,48): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(66,68,66,79): error CS0117: 'Resource.Attribute' does not contain a definition for 'ColorAccent'
CustomButtonCompatRenderer.cs(67,52,67,68): error CS0234: The type or namespace name 'Graphics' does not exist in the namespace 'CustomRenderer.Android' (are you missing an assembly reference?)
CustomButtonCompatRenderer.cs(75,69,75,81): error CS0117: 'Resource.Attribute' does not contain a definition for 'StatePressed'
My environment:
Visual studio for mac: 7.3.3 (build 12)
Android SDK tools 26.1.1
Android SDK build-tools 26.0.3
Android SDK build-tools 25.0.3
Android Target version: Android 7.1 (API 25)
Minimum Android version: Android 4.0.3 (API 15)
PS: I am new to Xamarin and C#