1
votes

I have a custom Entry for my Xamarin.Forms Android app. Currently, I'm using a Custom Renderer to give the Entry an oval shape with a border.

I also want to change the color of the Entry Border on focus and revert back to the original color on unfocus.

My custom Renderer is below:

public class EntryCellRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        var UIEntry = (e.NewElement != null) ? (EntryCell)e.NewElement : (EntryCell)e.OldElement;

        if (this.Control != null)
        {
            Control.Gravity = Android.Views.GravityFlags.CenterVertical;
            Control.SetPadding(30, 30, 30, 31);
            GradientDrawable gd = new GradientDrawable();
            gd.SetShape(ShapeType.Rectangle);

            var BackgroundColor = ColorHelper.FromHex(CoreTheme.COLOR_DEFAULT_CLEAR);
            var ColorRef = Android.Graphics.Color.Argb(
                (byte)(BackgroundColor.A * 255),
                (byte)(BackgroundColor.R * 255),
                (byte)(BackgroundColor.G * 255),
                (byte)(BackgroundColor.B * 255));


            gd.SetColor((int)ColorRef);

            UIEntry.BackgroundColor = Xamarin.Forms.Color.Transparent;
            gd.SetStroke(7, Android.Graphics.Color.LightGray);
            gd.SetCornerRadius(20.0f);
            Control.SetBackground(gd);

        }
    }
}

I'm not sure how to proceed with having a focus event for what I want to do above with this custom Entry.

2

2 Answers

2
votes

In your OnElementChanged you can wire up an event to the Focused event:

e.NewElement.Unfocused += (sender, evt) =>
{
    // unfocused, set color
};
e.NewElement.Focused += (sender, evt) =>
{
    // focus, set color
};

FYI: The OnNativeFocusChanged would be the perfect place to do this with an override, but it is not public...

internal virtual void OnNativeFocusChanged(bool hasFocus)
{
}
0
votes

You could wire up an Action to be run when ever your Focus event gets called. So you would add a new Action property to your custom EntryCell and also wire up the Focus:

public class CustomEntryCell : EntryCell {

    public Action OnFocusEventAction { get; set; }

    public CustomEntryCell() { Focused += OnFocused }

    private void OnFocused(object sender, FocusEventArgs focusEventArgs) { OnFocusEventAction?.Invoke(); }

    ///Or if you are not at C# 6 yet:
    //private void OnFocused(object sender, FocusEventArgs focusEventArgs) {
    //    Action action = OnFocusEventAction;
    //    if(action != null) { action.Invoke(); }
    //}
}

Then you Custom Renderer would assign something to the control's Action:

public class EntryCellRenderer : EntryRenderer {
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {
        ....
        UIEntry.OnFocusEventAction = () => //Your custom border color change code here
        ....
    }
}

Wrote this all out of memory and in this text box so if something does not work let me know and I will spend more time on it!