Ricky
Ricky

Reputation: 179

How to know which word is underlined?

I can underlined a word in edittext box using this method:

contentText.getEditableText().setSpan(new UnderlineSpan(), position, endLen, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

After i underlined the word, is there any methods that can let me know which word is underlined?This is because I need to know how many words and what is the word that had underlined. Thanks in advance for answering my question.

Upvotes: 0

Views: 1235

Answers (2)

PH7
PH7

Reputation: 3916

You can use getSpanEnd(Object tag) and getSpanStart(Object tag) to determine the start and end of the span. The only thing is u need to have reference to original UnderlineSpan Object.

Here is how I would do it.

UnderlineSpan span = new UnderlineSpan();
contentText.getEditableText().setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Log.d("Span", "___Start___"+contentText.getEditableText().getSpanStart(span));
Log.d("Span", "___End___"+contentText.getEditableText().getSpanEnd(span));

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006674

After i underlined the word, is there any methods that can let me know which word is underlined?

You already know what word is underlined. You just underlined it. Use position and endLen to retrieve the text that you underlined.

While you can call getSpans() later on to retrieve the UnderlineSpan, the UnderlineSpan does not contain information about its position.

Upvotes: 2

Related Questions