Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird TagHandler behavior detecting opening and closing tags

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?

like image 573
darklord Avatar asked May 09 '14 15:05

darklord


1 Answers

I solved this problem by adding to the beginning of the string "zero width joiner"

String looks like:

"&zwj;<articlelink>text1</articlelink>padding<articlelink>text2</articlelink>"

In result textview, this symbol is not visible and text looks like the original string

like image 166
user2536544 Avatar answered Oct 07 '22 01:10

user2536544