Skip to content

Instantly share code, notes, and snippets.

@mlakkadshaw
Created July 12, 2013 11:19
Show Gist options
  • Save mlakkadshaw/5983704 to your computer and use it in GitHub Desktop.
Save mlakkadshaw/5983704 to your computer and use it in GitHub Desktop.
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
Copy link

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

@JamesEdSmith
Copy link

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

@goofyz
Copy link

goofyz commented Jun 27, 2014

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

@naveenprincep
Copy link

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
Copy link

mdlalit commented Sep 1, 2016

can we make the bullet a bit larger ?

@tasneembohra
Copy link

tasneembohra 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
Copy link

francisrod01 commented Mar 17, 2017

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

@isaacriley
Copy link

isaacriley commented Nov 3, 2023

I'm trying to manipulate a custom tag. I have <J> tag and I want to change the color of the text within the tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy