How to set the color of the UnderlineSpan?

This question is a continuation of my last question: How to select text, for example, Android spell checking?

I am using UnderlineSpan to underline text in an EditText, but I need the underline to be COLORED , like this screenshot of the spellchecker :

enter image description here

+6
source share
3 answers

Unfortunately, you cannot do this exclusively with Span - there are no public methods to do what you ask. There is a way to set the underline color in TextPaint regardless of the color of the text (you can see it in the source code for SuggestionSpan), but it is hidden .

If you can access EditText, you can handle the Span and draw the underline yourself, but otherwise you're out of luck.

+2
source

here is a function you can use to set the color of the line below the 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); } } 
+1
source

This is how i did the job

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

All Articles