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
To change color you can use below code
editText.getBackground().mutate().setColorFilter(your_color), PorterDuff.Mode.SRC_ATOP);
And to set different color underline for EditText view on focus change
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_purple), PorterDuff.Mode.SRC_ATOP);
}else {
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_dark), PorterDuff.Mode.SRC_ATOP);
}
}
});
Modify your code in XfxEntryRendererDroid
ControlOnFocusChange
method like this :
private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
_hasFocus = args.HasFocus;
if (_hasFocus)
{
...
EditText.PostDelayed(() =>
{
//Add the following code
SetUnderlineColor(GetActivePlaceholderColor());
EditText.RequestFocus();
manager.ShowSoftInput(EditText, 0);
},
0);//Change it to 0
}
...
}
Effect.
Why don't you change the tint colour at runtime using this (May be in your text changed event):
ViewCompat.SetBackgroundTintList(_YourView , ColorStateList.ValueOf(Color.ParseColor(Resources.GetString(Resource.Color.blueLine))));
Anyways Goodluck!
So the solution for me was to go pure AppCompat
So I'm adding an AppCompatEditText
to the TextInputLayout
protected EditText EditText => Control.EditText;
protected override TextInputLayout CreateNativeControl()
{
var textInputLayout = new TextInputLayout(Context);
var editText = new AppCompatEditText(Context)
{
SupportBackgroundTintList = ColorStateList.ValueOf(GetPlaceholderColor())
};
textInputLayout.AddView(editText);
return textInputLayout;
}
Then from there I was able to set the underline consistently with this.
private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
_hasFocus = args.HasFocus;
SetUnderlineColor(_hasFocus ? GetActivePlaceholderColor(): GetPlaceholderColor());
}
private void SetUnderlineColor(AColor color)
{
var element = (ITintableBackgroundView)EditText;
element.SupportBackgroundTintList = ColorStateList.ValueOf(color);
}
full source code here.
You need to set the backgroundTintList or supportBackgroundTintList on the EditText to an instance of ColorStateList
ColorStateList colorStateList = ColorStateList.valueOf(color)
editText.setSupportBackgroundTintList(colorStateList)
OR
I think that if you want to change a bottom line color, then you can change using this below line
editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);
And Application theame like this:-
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorAccent</item>
</style>
Please check this Example
Hope this Link1 Link2 helps you.