Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.mohammedlakkadshaw.ken;
import java.util.Vector;
import org.xml.sax.XMLReader;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.style.BulletSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.TypefaceSpan;
import android.util.Log;
public class HtmlTagHandler implements Html.TagHandler {
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("ul") || tag.equals("ol") || tag.equals("dd")) {
if (opening) {
mListParents.add(tag);
} else mListParents.remove(tag);
mListItemCount = 0;
} else if (tag.equals("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);
}
}
}
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("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("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 * mListParents.size()), start, output.length(), 0);
}
}
}
@dschuermann

This comment has been minimized.

Copy link

commented Sep 2, 2013

Are you okay with publishing this under the Apache License v2?

@JamesEdSmith

This comment has been minimized.

Copy link

commented May 5, 2014

Trying to use this on anything gives me a java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary

@goofyz

This comment has been minimized.

Copy link

commented Jun 27, 2014

I got the exception java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary as well.

@naveenprincep

This comment has been minimized.

Copy link

commented Nov 1, 2014

I got the exception java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary.

I think its coming from this line
output.setSpan(new BulletSpan(15 * mListParents.size()), start, output.length(), 0);
and
output.setSpan(new LeadingMarginSpan.Standard(15 * mListParents.size()), start, output.length(), 0);

If you comment it and run it, it is working

@mdlalit

This comment has been minimized.

Copy link

commented Sep 1, 2016

can we make the bullet a bit larger ?

@tasneembohra

This comment has been minimized.

Copy link

commented Sep 28, 2016

Hello, Is there any way I can handle <font> tag in HtmlTagHandler as it is already being handled by Html.fromHtml() ? I want to handle 'size' in <font> tag but I do not want to use <h1> or <big> tags.

@francisrod01

This comment has been minimized.

Copy link

commented Mar 17, 2017

Me too @tasneembohra, I need to manipulate font tags h1, h2, a, br.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.