Tristan
Tristan

Reputation: 1790

Xamarin.Android Change Single EditText Underline Color

I am trying to change the holo underline color of a single EditText programmatically. I have tried all the examples I could find on SO, but nothing seems to work. Here is my latest and best attempt:

EDIT: Current code:

txtName.Background.ClearColorFilter();
txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);

I have also tried just using txtName.Background.SetTint(Resource.Color.colorRed) but that did not work either.

Here is a picture of the line color I am trying to change: enter image description here

EditText XML:

<EditText
        android:id="@+id/input_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textCapWords"
        android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
        android:maxLength="30"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:hint="Name"
        android:textColor="#8c8c8c"
        android:textColorHint="#8c8c8c"
        app:backgroundTint="#22d6d3"
        android:layout_marginBottom="10dp" />

EDIT - Here is the code that ended up working:

ViewCompat.SetBackgroundTintList(txtName, Android.Content.Res.ColorStateList.ValueOf(Color.Red));

Upvotes: 3

Views: 2115

Answers (4)

Pragnesh Mistry
Pragnesh Mistry

Reputation: 416

You can create the custom renderer for format the Style of EditText

 protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);

            }
        }

Upvotes: 0

FreakyAli
FreakyAli

Reputation: 16449

Update Only works if you use Appcompat Library.

I would suggest you give this a try

ViewCompat.SetBackgroundTintList(_YourView , ColorStateList.ValueOf(Color.ParseColor(#ff0000))));

In your case, _YourView means your EditText whose colour you want to change and value of takes Android graphics colour so it's easy to use

Another suggestion would be to use appcompat EditText if you are gonna support Android API-19 or below.

Upvotes: 1

Robbit
Robbit

Reputation: 4358

Thanks for @Large's answer, his answer works well on native Android using java.

In Xamarin.Android, it is same as Native Android, do like this:

txtName.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);

Or use android's color resource, like this:

txtName.Background.SetColorFilter(Color.Red, PorterDuff.Mode.SrcIn);

Don't use new Color(Resource.Color.colorRed), because the method getResources().getColor(int id) is deprecated in Android.

Upvotes: 0

Large
Large

Reputation: 717

As stated in this post: EditText underline below text property

To set the color:

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

To remove the color:

editText.getBackground().clearColorFilter();

There is also other variations like How to consistently set EditText Selected Underline Color Programatically I have tested the setColorFilter and it worked for my app.

Upvotes: 0

Related Questions