I'm trying to build a renderer for Xamarin Forms. The renderer needs to set the EditText
underline color to "Active Color" when selected and "Hint Color" when deselected. My initial setup looks something like this.
note: here's the path to the full source file
https://github.com/XamFormsExtended/Xfx.Controls/blob/issue-%236/src/Xfx.Controls.Droid/Renderers/XfxEntryRendererDroid.cs
// called when control is created or when Colors are changed.
protected virtual void SetLabelAndUnderlineColor()
{
var defaultColor = GetPlaceholderColor();
var activeColor = GetActivePlaceholderColor();
SetHintLabelDefaultColor(defaultColor);
SetHintLabelActiveColor(activeColor);
SetUnderlineColor(_hasFocus ? activeColor : defaultColor);
}
private void SetUnderlineColor(AColor color)
{
var bg = ColorStateList.ValueOf(color);
ViewCompat.SetBackgroundTintList(EditText,bg);
}
private void SetHintLabelActiveColor(AColor color)
{
var hintText = Control.Class.GetDeclaredField("mFocusedTextColor");
hintText.Accessible = true;
hintText.Set(Control, new ColorStateList(new int[][] { new[] { 0 } }, new int[] { color }));
}
private void SetHintLabelDefaultColor(AColor color)
{
var hint = Control.Class.GetDeclaredField("mDefaultTextColor");
hint.Accessible = true;
hint.Set(Control, new ColorStateList(new int[][] { new[] { 0 } }, new int[] { color }));
}
Outside of this, I also have an OnClickListener that updates the underline only when the state changes
private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
_hasFocus = args.HasFocus;
SetUnderlineColor(args.HasFocus ? GetPlaceholderColor() : GetActivePlaceholderColor());
}
The problem is that when I tap into the EditText, it's hit or miss as to what I'm going to see for an underline color.
I can pretty much guarantee that it'll be the default android:colorAccent
the very first time. Then after that it switches between the "Hint Color" and the "Placeholder Color".
note: if I change the SetUnderlineColor
method to this (below), it no longer uses the "Hint Color" in the mix, but I still get the android:colorAccent
color as the initial underline color, after that it behaves as expected.
private void SetUnderlineColor(AColor color)
{
var bg = EditText.Background;
DrawableCompat.SetTint(bg,color);
EditText.SetBackground(bg);
}
What do I need to do to set the INITIAL selected color of the EditText to my chosen activeColor
/ 'focused color' (purple)?
In this animation I am simply selecting and deselecting the EditText