3

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));
Tristan
  • 1,608
  • 1
  • 20
  • 34

4 Answers4

1

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.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
0

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.

Large
  • 587
  • 4
  • 16
  • tried this in my code: `txtName.Background.SetColorFilter(new Color(Resource.Color.colorRed), PorterDuff.Mode.SrcIn);` But still doesn't change the color. Do I need to clear the color before I add a new one? – Tristan Mar 06 '18 at 01:17
0

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.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • I used your exact code and ran on an emulator running API 21. Still didn't change the color. I have also set breakpoints to make sure that the code is being hit correctly, which it is. Back to my question from the other answer, do i need to clear the color filter before adding the red? – Tristan Mar 06 '18 at 03:09
  • You can use `txtName.Background.ClearColorFilter();` to try it, but I am not sure about it, can you show me your `EditText` both code and `.axml`? – Robbit Mar 06 '18 at 03:13
  • Yeah, clearing didn't work either. I updated the post with the XML and the current code. – Tristan Mar 06 '18 at 03:29
  • Hi, we are using `EditText` not `AppCompatEditText`, please try the `EditText` class on you min version and other version. – Robbit Mar 06 '18 at 03:30
  • Changed it to the regular EditText.. still nothing. EDIT: Just to clarify I also made sure and used EditText as the type in the code. – Tristan Mar 06 '18 at 03:38
  • Can you create a new project to test it? If it still not work, would you like to push you project to the github? – Robbit Mar 06 '18 at 03:46
  • I started a new project and installed the Support v7 package. Added a couple of edit texts for testing and I still can't get the color to change from the initial value. I used `oText.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#ff0000"), PorterDuff.Mode.SrcIn);` as my code. – Tristan Mar 06 '18 at 04:31
  • Is it an issue with the support v7 library? Without it, the text boxes don't look right, and I can't even get the initial line color to change. Just the background – Tristan Mar 06 '18 at 04:33
  • By `Support v7 package`, is [this](https://www.nuget.org/packages/Xamarin.Android.Support.v7.AppCompat/)? – Robbit Mar 06 '18 at 05:06
  • Yes. That is correct. I am also using v4 for a few things as well, which is what @G.hakim used in his answer. And it works! Thanks for your help. – Tristan Mar 06 '18 at 18:03
0

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

            }
        }
Pragnesh Mistry
  • 386
  • 5
  • 13