I am trying to use TextView to display a String text with custom tags:
The String:
"<articlelink>text1</articlelink> padding<articlelink>text2</articlelink>"
Where articlelink is a custom tag. I use a customized HTML.TagHandler to handle the tags:
private class MyTagHandler implements Html.TagHandler {
private int startIndex = 0;
private int endIndex = 0;
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (tag.equals("articlelink")) {
if (opening) {
startIndex = output.length();
DebugLog.d("OPEN " + startIndex);
} else {
endIndex = output.length();
DebugLog.d("END " + endIndex);
MyClickableSpan span = new MyClickableSpan();
output.setSpan(span, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
However the log is:
OPEN 0
OPEN 13
END 18
END 18
However, after I insert a character before the string then the output is what I expected:
String:
"a<articlelink>text1</articlelink> padding<articlelink>text2</articlelink>"
OUTPUT:
OPEN 1
END 6
OPEN 14
END 19
What happened here? Is this a bug or I misused it?
I solved this problem by adding to the beginning of the string "zero width joiner"
String looks like:
"‍<articlelink>text1</articlelink>padding<articlelink>text2</articlelink>"
In result textview, this symbol is not visible and text looks like the original string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With