Today I am creating another new post in which I am going to show you how to customize the button, so let's start,

So we will first create a class whose name "CustomButtons" and then we will write a code in "C #"

  1. public class CustomButton: Button
  2. {
  3. public static readonly BindableProperty CustomBorderColorProperty =
  4. BindableProperty.Create(
  5. nameof(CustomBorderColor),
  6. typeof(Color),
  7. typeof(CustomButton),
  8. Color.Default);
  9. public Color CustomBorderColor
  10. {
  11. get { return (Color)GetValue(CustomBorderColorProperty); }
  12. set { SetValue(CustomBorderColorProperty, value); }
  13. }
  14. public static readonly BindableProperty CustomBorderRadiusProperty =
  15. BindableProperty.Create(
  16. nameof(CustomBorderRadius),
  17. typeof(int),
  18. typeof(CustomButton),
  19. 4);
  20. public int CustomBorderRadius
  21. {
  22. get { return (int)GetValue(CustomBorderRadiusProperty); }
  23. set { SetValue(CustomBorderRadiusProperty, value); }
  24. }
  25. public static readonly BindableProperty CustomBorderWidthProperty =
  26. BindableProperty.Create(
  27. nameof(CustomBorderWidth),
  28. typeof(double),
  29. typeof(CustomButton),
  30. 4.0);
  31. public double CustomBorderWidth
  32. {
  33. get { return (double)GetValue(CustomBorderWidthProperty); }
  34. set { SetValue(CustomBorderWidthProperty, value); }
  35. }
  36. public static readonly BindableProperty CustomBackgroundColorProperty =
  37. BindableProperty.Create(
  38. nameof(CustomBorderColor),
  39. typeof(Color),
  40. typeof(CustomButton),
  41. Color.Default
  42. );
  43. public Color CustomBackgroundColor
  44. {
  45. get { return (Color)GetValue(CustomBackgroundColorProperty); }
  46. set { SetValue(CustomBackgroundColorProperty, value); }
  47. }
  48. }

Now we can write some code in Android project for rendering and setting the Border Color, Width, Radius and Background Color....

  1. [assembly: ExportRenderer(typeof(CustomButton), typeof(CurvedCornersButtonRenderer))]
  2. namespace MyNewApp.Droid.CustomRenderer
  3. {
  4. public class CurvedCornersButtonRenderer: ButtonRenderer
  5. {
  6. private GradientDrawable _gradientBackground;
  7. protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
  8. {
  9. base.OnElementChanged(e);
  10. var view = (CustomButton)Element;
  11. if (view == null) return;
  12. Paint(view);
  13. }
  14. protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  15. {
  16. base.OnElementPropertyChanged(sender, e);
  17. if(e.PropertyName == CustomButton.CustomBorderColorProperty.PropertyName ||
  18. e.PropertyName == CustomButton.CustomBorderRadiusProperty.PropertyName ||
  19. e.PropertyName == CustomButton.CustomBorderWidthProperty.PropertyName)
  20. {
  21. if (Element != null)
  22. {
  23. Paint((CustomButton)Element);
  24. }
  25. }
  26. }
  27. private void Paint(CustomButton view)
  28. {
  29. _gradientBackground = new GradientDrawable();
  30. _gradientBackground.SetShape(ShapeType.Rectangle);
  31. _gradientBackground.SetColor(view.CustomBackgroundColor.ToAndroid());
  32. // Thickness of the stroke line
  33. _gradientBackground.SetStroke((int)view.CustomBorderWidth, view.CustomBorderColor.ToAndroid());
  34. // Radius for the curves
  35. _gradientBackground.SetCornerRadius(
  36. DpToPixels(this.Context,Convert.ToSingle(view.CustomBorderRadius)));
  37. // set the background of the label
  38. Control.SetBackground(_gradientBackground);
  39. }
  40. public static float DpToPixels(Context context, float valueInDp)
  41. {
  42. DisplayMetrics metrics = context.Resources.DisplayMetrics;
  43. return TypedValue.ApplyDimension(ComplexUnitType.Dip, valueInDp, metrics);
  44. }
  45. }
  46. }

Then we do this:

  1. [assembly: ExportRenderer(typeof(CustomButton), typeof(CurvedCornersButtonRenderer))]
  2. namespace MyNewApp.iOS.CustomRenderer
  3. {
  4. public class CurvedCornersButtonRenderer: ButtonRenderer
  5. {
  6. protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
  7. {
  8. base.OnElementChanged(e);
  9. var view = (CustomButton)Element;
  10. if (view == null) return;
  11. Paint(view);
  12. }
  13. protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  14. {
  15. base.OnElementPropertyChanged(sender, e);
  16. if (e.PropertyName == CustomButton.CustomBorderRadiusProperty.PropertyName ||
  17. e.PropertyName == CustomButton.CustomBorderColorProperty.PropertyName||
  18. e.PropertyName == CustomButton.CustomBorderWidthProperty.PropertyName )
  19. {
  20. if (Element != null)
  21. {
  22. Paint((CustomButton)Element);
  23. }
  24. }
  25. }
  26. private void Paint(CustomButton view)
  27. {
  28. this.Layer.CornerRadius = (float)view.CustomBorderRadius;
  29. this.Layer.BorderColor = view.CustomBorderColor.ToCGColor();
  30. this.Layer.BackgroundColor = view.CustomBackgroundColor.ToCGColor();
  31. this.Layer.BorderWidth = (int)view.CustomBorderWidth;
  32. }
  33. }
  34. }

Now again please make sure to add view reference.....

xmlns:local="clr-namespace:CustomButtonApp" then write xaml code in main page.

  1. <ContentPage.Content>
  2. <StackLayout VerticalOptions="StartAndExpand" Padding="20,5" Spacing="10">
  3. <customview:CustomButton x:Name="btnSignin"
  4. CustomBorderColor="#24C4FF"
  5. CustomBackgroundColor="#24C4FF"
  6. Text="Log In" TextColor="White"
  7. Clicked="btnSignin_Clicked"
  8. CustomBorderRadius="4"
  9. CustomBorderWidth="4"
  10. VerticalOptions="Center"/>
  11. </StackLayout>
  12. </ContentPage.Content>

You should have your CustomButton working!!

If You want to watch related video click Here Custom Rendered Button

Features of CustomButton controls
  1. Custom Border Color Property=(CustomBorderColor="#24C4FF")
  2. Custom Background Color Property=(CustomBackgroundColor="#24C4FF")
  3. Custom Border Radius Property=(CustomBorderRadius="4")
  4. Custom Border Width Property=(CustomBorderWidth="4")

NOTE

IN THE SOURCE CODE , ONLY HAVE CODE PLZ DOWNLOAD REFERENCE

In the new version Of Xamarin Studio or Visual Studio(4.7.10.38) you don't need to customize these properties, this property is working well in the new version (4.7.10.38).