Created
November 29, 2018 07:49
-
-
Save vishalhalani/074956fafa635c727cef517963b03749 to your computer and use it in GitHub Desktop.
html tag handler when to show bulleted points in textview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HtmlTagHandler implements Html.TagHandler { | |
String parent = null; | |
boolean first = true; | |
int index = 1; | |
private int mListItemCount = 0; | |
private Vector<String> mListParents = new Vector<String>(); | |
@Override | |
public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) { | |
if (tag.equals(Util.UL) || tag.equals(Util.OL) || tag.equals("dd")) { | |
if (opening) { | |
mListParents.add(tag); | |
} else mListParents.remove(tag); | |
mListItemCount = 0; | |
} else if (tag.equals(Util.LI) && !opening) { | |
handleListTag(output); | |
} else if (tag.equalsIgnoreCase("code")) { | |
if (opening) { | |
output.setSpan(new TypefaceSpan("monospace"), output.length(), output.length(), Spannable.SPAN_MARK_MARK); | |
} else { | |
Log.d("COde Tag", "Code tag encountered"); | |
Object obj = getLast(output, TypefaceSpan.class); | |
int where = output.getSpanStart(obj); | |
output.setSpan(new TypefaceSpan("monospace"), where, output.length(), 0); | |
} | |
} | |
// if (tag.equals(Util.OL)) parent = "ol"; | |
// if (tag.equals(Util.LI)) { | |
// if (parent.equals("ul")) { | |
// if (first) { | |
// output.append("\n\t•"); | |
// first = false; | |
// } else { | |
// first = true; | |
// } | |
// } else { | |
// if (first) { | |
// output.append("\n\t" + index + ". "); | |
// first = false; | |
// index++; | |
// } else { | |
// first = true; | |
// } | |
// } | |
// } | |
// | |
} | |
private Object getLast(Editable text, Class kind) { | |
Object[] objs = text.getSpans(0, text.length(), kind); | |
if (objs.length == 0) { | |
return null; | |
} else { | |
for (int i = objs.length; i > 0; i--) { | |
if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) { | |
return objs[i - 1]; | |
} | |
} | |
return null; | |
} | |
} | |
private void handleListTag(Editable output) { | |
if (mListParents.lastElement().equals(Util.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 * mListParents.size()), start, output.length(), 0); | |
} else if (mListParents.lastElement().equals(Util.OL)) { | |
mListItemCount++; | |
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, mListItemCount + ". "); | |
output.setSpan(new LeadingMarginSpan.Standard(15 * 2), start, output.length(), 0); | |
} else if (mListParents.lastElement().equals(Util.PR)) { | |
mListItemCount++; | |
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 LeadingMarginSpan.Standard(15 * 2), start, output.length(), 0); | |
} | |
} | |
} | |
******************************************* HOW TO USE ****************************************************** | |
String info = getHtmlText(infoArray); | |
String html = Util.customizeListTags(info); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
tvInfo.setText(Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY, null, new HtmlTagHandler())); | |
} else { | |
//noinspection deprecation | |
tvInfo.setText(Html.fromHtml(html, null, new HtmlTagHandler())); | |
} | |
******************************************************* USEFUL ******************************************* | |
public String getHtmlText(String[] s) { | |
StringBuilder ulStart = new StringBuilder("<ul>"); | |
for (String value : s) { | |
ulStart.append("<li>").append(value).append("</li>"); | |
} | |
ulStart.append("</ul>"); | |
return ulStart.toString(); | |
} | |
public static String customizeListTags(@Nullable String html) { | |
if (html == null) { | |
return null; | |
} | |
html = html.replace("<ul", "<" + UL); | |
html = html.replace("</ul>", "</" + UL + ">"); | |
html = html.replace("<ol", "<" + OL); | |
html = html.replace("</ol>", "</" + OL + ">"); | |
// html = html.replace("<dd", "<" + DD); | |
// html = html.replace("</dd>", "</" + DD + ">"); | |
html = html.replace("<li", "<" + LI); | |
html = html.replace("</li>", "</" + LI + ">"); | |
return html; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment