Euridice01 Euridice01 - 1 year ago 252
Android Question

Change border color of Entry on focus

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)
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();

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));


UIEntry.BackgroundColor = Xamarin.Forms.Color.Transparent;
gd.SetStroke(7, Android.Graphics.Color.LightGray);


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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download