Support List tag of HTML in TextView using fromHtml()

As we know Html class processes HTML strings into displayable styled text.
It is not all HTML tags are supported.

There is a lovely method on the android.text.Html class, fromHtml(), that converts HTML into a Spannable for use with a TextView.
Here I found Tags list which is supported as of Android 2.1:

There is not supporting ul and li tags to display list but I found one Snippet while finding solution for List Tag supporting in TextView that i want to share with you all so it may help to other developers.

Just copy and paste following snippet whole class in as Java file in your project and use that class in parameterized:
public static Spanned fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Snippet for ListTagHandler:
/**
 * @author Edited By: Pratik Butani
 * @version 1.0
 * @category HTML List in TextView
 * {@link http://stackoverflow.com/a/16169511/1318946}
 */

public class ListTagHandler implements TagHandler {
   
private int             index   = 0;
   
private List<String>    parents = new ArrayList<String>();

   
/* (non-Javadoc)
     * @see android.text.Html.TagHandler#handleTag(boolean, java.lang.String, android.text.Editable, org.xml.sax.XMLReader)
     */

   
@Override
   
public void handleTag(final boolean opening, final String tag, Editable output,
                   
final XMLReader xmlReader) {
       
if (tag.equals("ul") || tag.equals("ol") || tag.equals("dd")) {
           
if (opening) {
                parents
.add(tag);
           
} else
            parents
.remove(tag);

                index
= 0;
           
} else if (tag.equals("li") && !opening)
                handleListTag
(output);
   
}

   
/**
     * @param output
     */

   
private void handleListTag(Editable output) {
       
if (parents.get(parents.size() - 1).equals("ul")) {
            output
.append("n");
         
String[] split = output.toString().split("n");

           
int lastIndex = split.length - 1;
           
int start = output.length() - split[lastIndex].length() - 1;
            output
.setSpan(new BulletSpan(15 * parents.size()), start, output.length(), 0);
       
} else if (parents.get(parents.size() - 1).equals("ol")) {
        index
++;

            output
.append("n");
           
String[] split = output.toString().split("n");

           
int lastIndex = split.length - 1;
           
int start = output.length() - split[lastIndex].length() - 1;
            output
.insert(start, index + ". ");
            output
.setSpan(new LeadingMarginSpan.Standard(15 * parents.size()), start, output.length(), 0);
       
}
   
}
}
How to use:
TextView textView = (TextView) findViewById(R.id.textView1);
//class ListTagHandler will be used here.
textView.setText(Html.fromHtml(yourHtml, null, new ListTagHandler()));
Keep Sharing…
Thanks 🙂

Page Views (59)

644 Total Views 1 Views Today
Share Love & Support:

About Pratik Butani

Pratik Butani is Enthusiastic Android Application Developer, Speaker at Google Developer Group – Rajkot, All time Learner of new things, Googler, Eager to Help IT Peoples. He also like to TEACH Android to New Learner and Share his knowledge to them. He is currently working as a Senior Android Developer at Soham ERP Solutions Pvt. Ltd. – Ahmedabad. He is working with Android Development since 2013 and now Flutter is on going.

Comment Your Suggestion or Appreciation:

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *