Question

spanned = Html.fromHtml("<sup>aaa</sup>bbb<sub>ccc</sub><b>ddd</b>");

Will create a Spanned object with with 3 spans aaa, ccc, ddd.

bbb is being ignored since it's not inside an html tag,

spans = spanned.getSpans(0, spanned.length(), Object.class);

will only identify 3 spans.

I need a way to extract all the 4 sections of the code, if possible into some sort of an array that will allow to me to identify the the type of each span.

Was it helpful?

Solution

I need a way to extract all the 4 sections of the code

Use nextSpanTransition() to find the starting point of the next span. The characters between your initial position (first parameter to nextSpanTransition()) and the next span represent an unspanned portion of text.

You can take a look at the source code to the toHtml() method on the Html class to see this in action.

OTHER TIPS

'bbb' is the one which is not inside html tag. Even though i guess it will not be missed. 'ccc' is a subscript, may be it is rendered but not visible to you. Try to increase textview height if you have constrained it.

use this http://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, android.text.Html.ImageGetter, android.text.Html.TagHandler), pass null for ImageGetter and your custom TagHandler

see the example

String source = "<b>bold</b> <i>italic</i> <unk>unknown</unk>";
TagHandler tagHandler = new TagHandler() {
    Stack<Integer> starts = new Stack<Integer>();
    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.equals("unk")) {
            if (opening) {
                starts.add(output.length());
            } else {
                int start = starts.pop();
                int end = output.length();
                Object what = new Object();
                output.setSpan(what, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
};
Spanned spanned = Html.fromHtml(source, null, tagHandler);
TextUtils.dumpSpans(spanned, new LogPrinter(Log.DEBUG, TAG), "span ");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top