How to change Border Color of Entry in Xamarin.Forms

31,791

I think you can only achieve this with a CustomRenderer:

iOS:

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
       base.OnElementPropertyChanged(sender, e);

       Control.Layer.BorderColor = UIColor.Red.CGColor;
       Control.Layer.BorderWidth = 1;
}

On Android, I think it's not possible without a CustomRender (Actually, if it is... I don't know how ~ Sorry):

Using the CustomRenderer would be something like this:

    [assembly: ExportRenderer(typeof(Entry), typeof(SuperEntryRenderer))]
    namespace Bla{
    public class SuperEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (e.OldElement == null)
                {
                    var nativeEditText = (global::Android.Widget.EditText)Control;
                    var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
                    shape.Paint.Color = Xamarin.Forms.Color.Red.ToAndroid();
                    shape.Paint.SetStyle(Paint.Style.Stroke);
                    nativeEditText.Background = shape;
                }
            }
        }
Share:
31,791
Lazare Giunashvili
Author by

Lazare Giunashvili

Updated on July 09, 2022

Comments

  • Lazare Giunashvili
    Lazare Giunashvili almost 2 years

    I'm writing an app in Xamarin.forms cross-platform. There are few Entries in The app and I want to create/change border color to red. Is There any easy way to do this? or Does any way exists?

  • DisplayName
    DisplayName over 6 years
    Thank you Rodrigo, this has been incredibly helpful to someone just delving into custom renderers. I am surprised at how much needs to be done in order to change the border color on android.