0

I'm trying to make a listview which has TextView Elements, each of which contains a SpannableString. The Content of these TextViews are fetched from an ArrayList in Html markup and Converted to SpannableStrings using Html.fromHtml. Now, Html.fromHtml has a lot of performance problems. So I tried to make my own Version of Html.fromHtml

class NeoHTML extends DefaultHandler {

SpannableStringBuilder s;
String html;
Context context;

public NeoHTML(String html,Context context) {
    s = new SpannableStringBuilder("");
    this.html = html;
    this.context=context;
    getXml();
}

public SpannableStringBuilder fromHTML() {
    return s;
}


public void getXml() {
    try {

        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();
        DefaultHandler defaultHandler = new DefaultHandler() {

            int boldTag = 0;
            int italicsTag = 0;
            int underlineTag = 0;
            int pointer = 0;
            boolean brpassed=false;
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {

                if (qName.equalsIgnoreCase("B")) {
                    boldTag += 1;
                }
                if (qName.equalsIgnoreCase("I")) {
                    italicsTag += 1;
                }
                if (qName.equalsIgnoreCase("U")) {
                    underlineTag += 1;
                }
            }

            public void characters(char ch[], int start, int length)
                    throws SAXException {
                int tstart=start;


                    if(new String(ch, start, length).startsWith("[ ]")){
                        s.append("b");
                        Typeface font = Typeface.createFromAsset(context.getAssets(), "tickfont.ttf");
                        s.setSpan (new CustomTypefaceSpan("", font),pointer, pointer+1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                        pointer+=1;
                        tstart+=3;
                    }

                    else if(new String(ch, start, length).startsWith("[*]")){
                        s.append("a");
                        Typeface font = Typeface.createFromAsset(context.getAssets(), "tickfont.ttf");
                        s.setSpan (new CustomTypefaceSpan("", font),pointer, pointer+1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                        pointer+=1;
                        tstart+=3;
                    }



                if (boldTag > 0 || italicsTag > 0 || underlineTag > 0) {
                    s.append(new String(ch, tstart, length));
                    if (boldTag > 0) {

                        s.setSpan(new StyleSpan(
                                android.graphics.Typeface.BOLD), pointer,
                                pointer + length,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    }
                    if (italicsTag > 0) {

                        s.setSpan(new StyleSpan(
                                android.graphics.Typeface.ITALIC), pointer,
                                pointer + length,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    }
                    if (underlineTag > 0) {

                        s.setSpan(new UnderlineSpan(), pointer, pointer
                                + length,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

                } else {
                    s.append(new String(ch, tstart, length));
                }

                pointer += length;
            }

            public void endElement(String uri, String localName,
                    String qName) throws SAXException {

                if (qName.equalsIgnoreCase("B")) {
                    boldTag -= 1;
                }
                if (qName.equalsIgnoreCase("BR")) {
                    s.append("\n");
                    pointer+=1;
                    brpassed=true;
                }
                if (qName.equalsIgnoreCase("I")) {
                    italicsTag -= 1;
                }
                if (qName.equalsIgnoreCase("U")) {
                    underlineTag -= 1;
                }
            }
        };
        saxParser.parse(new InputSource(new StringReader(html)),
                defaultHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

This is simple a sax based parser. I did this inspired by a previous answer by CommonsWare Is there a faster way to decode html characters to a string than Html.fromHtml()?. It has minimal functionality ( bold, italics, underline and breaks) But even so, performance is not that Improved. I have some ideas like compositing the textview to a bitmap and caching it in memory, and not having to re-render it again when the listview recycles. Can anyone suggest any Ideas? .. (please avoid NDK based solutions as I have never successfully compiled any them and It's adding unwanted complexity)

Community
  • 1
  • 1
Atul Vinayak
  • 466
  • 3
  • 15

1 Answers1

2

I had a simmilar issue a while back, when i did Html.fromHtml the text was blurry on some devices when it had to be bold. So this was my solution:

public static Spannable boldFromHtml(Context c, String text, boolean extraBold){
    String textTemp = text;

    int boldStartPos = 0;
    int boldEndPos = 1;
    List<int[]> spanList = new LinkedList<int[]>();

    while((boldStartPos = textTemp.indexOf("<b>", boldStartPos)) != -1 && (boldEndPos = textTemp.indexOf("</b>", boldStartPos) - 3) != -1){
        textTemp = textTemp.replaceFirst("<b>", "").replaceFirst("</b>", "");
        spanList.add(new int[]{boldStartPos, boldEndPos});
        boldStartPos += 3;
    }

    Spannable span = new SpannableString(textTemp);
    for(int[] selection: spanList){
        span.setSpan(new BoldTypefaceSpan("sans-serif", (extraBold) ? MyExtraBoldFont(c) : MyBoldFont(c)), selection[0], selection[1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return span;
}

This function only bolds your elements but it could easily be modified to do other tags.

Squeazer
  • 1,234
  • 1
  • 14
  • 33