Change focus color of Entry control in Xamarin Forms

Viewed 4672

How can you change the focus border and cursor color in a Entry control in Xamarin forms? In the emulator it is standard red?

I added this in my Android project

[assembly: ExportRenderer(typeof(CustomEntryControl), typeof(MyEntryRenderer))]
namespace MyApp.Droid
{
    public class MyEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
            }
        }
    }
}

But can't find the property for border or cursor?

enter image description here

3 Answers

You can change Entry focus color in Android Project in the style.xml file. The path is: Resources/values/styles.xml

Then, look at the "colorAccent" property to set the custom color.

ColorAccent in Xamarin Android Project

Hi, try this code to change iOS entry looks like native Android entry Add this renderer in your ios section ; also Focus and unfocus color is changing.

remember this line to add above namespace [assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]

public class CustomEntryRenderer : EntryRenderer
{
    private CALayer _borderLayer;
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Control.BorderStyle = UITextBorderStyle.None;

        var element = Element as Entry;
        if (element == null)
            return;

        //DrawBorder(element.BorderColor.ToCGColor());
        DrawBorder(UIColor.FromRGB(156, 156, 156));

        e.NewElement.Unfocused += (sender, evt) =>
        {
            DrawBorder(UIColor.FromRGB(156, 156, 156)); // unfocused, set color

        };
        e.NewElement.Focused += (sender, evt) =>
        {
            DrawBorder(UIColor.FromRGB(245, 0, 47)); ; // focused, set color
        };
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        var element = Element as Entry;
        if (element == null)
            return;

        DrawBorder(UIColor.FromRGB(156, 156, 156));
    }
    public override CGRect Frame
    {
        get { return base.Frame; }
        set
        {
            base.Frame = value;

            var element = Element as Entry;
            if (element == null)
                return;

            // DrawBorder(element.BorderColor.ToCGColor());
            DrawBorder(UIColor.FromRGB(156, 156, 156));
        }
    }
    private void DrawBorder(UIColor borderColor)
    {
        if (Frame.Height <= 0 || Frame.Width <= 0)
            return;

        if (_borderLayer == null)
        {
            _borderLayer = new CALayer
            {
                MasksToBounds = true,
                Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f),
                BorderColor = borderColor.CGColor,
                BorderWidth = 1.0f
            };

            Control.Layer.AddSublayer(_borderLayer);
            Control.BorderStyle = UITextBorderStyle.None;
        }
        else
        {
            _borderLayer.BorderColor = borderColor.CGColor;
            _borderLayer.Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f);
        }
    }
}
Related