title: Alternatives to Html.fromHtml?
body: <p>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 </p>
  
  <pre><code>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 &gt; 0 || italicsTag &gt; 0 || underlineTag &gt; 0) {
                      s.append(new String(ch, tstart, length));
                      if (boldTag &gt; 0) {
  
                          s.setSpan(new StyleSpan(
                                  android.graphics.Typeface.BOLD), pointer,
                                  pointer + length,
                                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  
                      }
                      if (italicsTag &gt; 0) {
  
                          s.setSpan(new StyleSpan(
                                  android.graphics.Typeface.ITALIC), pointer,
                                  pointer + length,
                                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  
                      }
                      if (underlineTag &gt; 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();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is simple a sax based parser. I did this inspired by a previous answer by CommonsWare <a href="https://stackoverflow.com/questions/4321896/is-there-a-faster-way-to-decode-html-characters-to-a-string-than-html-fromhtml">Is there a faster way to decode html characters to a string than Html.fromHtml()?</a>. 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)</p>