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 :)