12

This question is a follow up to my last question: How to highlight text like Android spell-checker?

I'm using an UnderlineSpan to underline text in an EditText but I need the underline to be IN COLOR like this screen shot of the spelling checker:

enter image description here

Community
  • 1
  • 1
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
  • This is probably the best you'll do: http://stackoverflow.com/questions/7684560/change-the-color-of-the-underline-in-android ... Or, perhaps: http://stackoverflow.com/questions/13238298/android-change-underline-color-from-an-edittext-dynamically – Cat Jan 26 '13 at 02:22
  • Thank you but unfortunately the first solution didn't work (both answers) and I cannot use the second since my app is a keyboard, which cannot access the EditText directly. – Barry Fruitman Jan 26 '13 at 02:44
  • The problem is that there is no API-supported method to do it, at least not on stock Android. – Cat Jan 26 '13 at 03:08
  • The only way I could find was to use a SuggestionSpan, which can be red, blue or light gray, but not any custom color. – Barry Fruitman Feb 20 '13 at 00:41
  • Check the @android-developer answer https://stackoverflow.com/a/48665758/2713403 – Jaco Feb 19 '21 at 17:12

4 Answers4

3

Unfortunately, you can't do this solely with a Span -- there are no public methods to do what you ask. There is a method to set the underline color in TextPaint independent of the text color (you can see it in the source code for SuggestionSpan) but it's hidden.

If you can get access to the EditText, you can handle the Span and draw the underline yourself, but otherwise you are out of luck.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Lisa Wray
  • 2,242
  • 16
  • 23
2

here is the function which you can use to set color of line below text

public void addUnderLineLink(TextView mTextView) {
    if (mTextView != null) {
        SpannableString content = new SpannableString(mTextView.getText().toString());
        UnderlineSpan us=new UnderlineSpan();
        TextPaint tp=new TextPaint();
        tp.setColor(ContextCompat.getColor(getContext(),R.color.white));
        us.updateDrawState(tp);
        content.setSpan(us, 0, mTextView.getText().toString().length(), 0);
        mTextView.setText(content);
    }
}
Navneet Kumar
  • 273
  • 1
  • 11
1

Since Android 10 (SDK 29) you can do this by setting underlineColor and underlineThickness of TextPaint.

Example:

val text = SpannableString("Your string")
text.setSpan(ColoredUnderlineSpan(Color.RED, 10f), 0, 4, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE)
textView.text = text

class ColoredUnderlineSpan constructor(
    @ColorInt private val underlineColor: Int,
    private val underlineThickness: Float,
) : CharacterStyle() {
    override fun updateDrawState(textPaint: TextPaint) {
        textPaint.underlineColor = underlineColor
        textPaint.underlineThickness = underlineThickness
    }
}
DropDrage
  • 725
  • 8
  • 9
0

This is how i made it work

Spanned part_1 = Html.fromHtml(getString(R.string.part1));
Spanned part_2 = Html.fromHtml("<u><font color='#2eb6f0'>Text in blue</font></u>");
Spanned part_3 = Html.fromHtml(getString(R.string.part3));
Spanned output = (Spanned) TextUtils.concat(part_1, part_2, part_3);

textview.setText(output, TextView.BufferType.SPANNABLE);
DeRagan
  • 22,827
  • 6
  • 41
  • 50