Background Color extends past Border Custom Entry Renderer

I created a simple custom entry renderer to add rounded corners and border color. However, when I set the background color of the custom entry, the fills the entire rectangular box, extending past the rounded corners. This only happens on Android.

Here is the Android renderer:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || e.NewElement == null)
                return;

            try
            {
                element = (EntryBox)this.Element;

                GradientDrawable gradientDrawable = new GradientDrawable();
                gradientDrawable.SetShape(ShapeType.Rectangle);
                gradientDrawable.SetColor(element.BackgroundColor.ToAndroid());
                gradientDrawable.SetStroke(4, element.BorderColor.ToAndroid());
                gradientDrawable.SetCornerRadius(DpToPixels(Context, (float)element.CornerRadius));

                Control.SetBackground(gradientDrawable);
                Control.SetPadding(
                    (int)DpToPixels(this.Context, Convert.ToSingle(12)), Control.PaddingTop,
                    (int)DpToPixels(this.Context, Convert.ToSingle(12)), Control.PaddingBottom);


            } catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }

Any help would be appreciated.

Posts

  • JeeepzJeeepz USMember ✭✭

    I just encountered this problem. And the solution I ended up using, was to just to hard code the background, and not setting the background on the XAML. You could just create a property on your CustomEntry, called NewBackgroundColor, and use that instead. Because it seems it doesn't overwrite the current BackgroundColor on the entry.

    using Color = Xamarin.Forms.Color;
    
    [assembly: ExportRenderer(typeof(FullBorderEntry), typeof(FullBorderEntryRenderer))]
    namespace Test.Droid
    {
    
        public class FullBorderEntryRenderer : EntryRenderer
        {
            public FullBorderEntryRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (e.NewElement != null)
                {                
                    FullBorderEntry view = (FullBorderEntry)Element;
                    //creating gradient drawable for the curved background.
                    var _gradientBackground = new GradientDrawable();
                    _gradientBackground.SetShape(ShapeType.Rectangle);
                    _gradientBackground.SetColor(Color.White.ToAndroid());
    
                    //Thickness of the stroke line.
                    _gradientBackground.SetStroke(view.BorderWidth, view.BorderColor.ToAndroid());
    
                    //Radius of the curves.
                    _gradientBackground.SetCornerRadius(DpToPixels(this.Context, Convert.ToSingle(view.BorderRadius)));
    
                    //Set the background of the label.
                    Control.SetBackground(_gradientBackground);
                }
    
                //Set padding for the internal text from border.
                Control.SetPadding(
                    (int)DpToPixels(this.Context, Convert.ToSingle(12)),
                    Control.PaddingTop,
                    (int)DpToPixels(this.Context, Convert.ToSingle(12)),
                    Control.PaddingBottom);
            }
    
            public static float DpToPixels(Context context, float valueInDp)
            {
                DisplayMetrics metrics = context.Resources.DisplayMetrics;
                return TypedValue.ApplyDimension(ComplexUnitType.Dip, valueInDp, metrics);
            }
        }
    }
    
Sign In or Register to comment.