GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 1
  • Stars 7
  • Forks 1
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — HtmlTagHandler.java

Instantly share code, notes, and snippets.

@mlakkadshaw

mlakkadshaw/HtmlTagHandler.java

Created July 12, 2013 11:19
Show Gist options
  • Download ZIP
  • Star 7 (7) You must be signed in to star a gist
  • Fork 1 (1) You must be signed in to fork a gist
  • Clone this repository at <script src="https://gist.github.com/mlakkadshaw/5983704.js"></script>
  • Save mlakkadshaw/5983704 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/mlakkadshaw/5983704.js"></script>
Save mlakkadshaw/5983704 to your computer and use it in GitHub Desktop.
Download ZIP
Raw
HtmlTagHandler.java
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

dschuermann commented Sep 2, 2013

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JamesEdSmith

JamesEdSmith commented May 5, 2014

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@goofyz

goofyz commented Jun 27, 2014

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@naveenprincep

naveenprincep commented Nov 1, 2014

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdlalit

mdlalit commented Sep 1, 2016

Copy link
Copy Markdown

can we make the bullet a bit larger ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tasneembohra

tasneembohra commented Sep 28, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@batistafrancis

batistafrancis commented Mar 17, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@isaacriley

isaacriley commented Nov 3, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.