Skip to content

Instantly share code, notes, and snippets.

@Ilya-Gazman
Last active February 19, 2018 21:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ilya-Gazman/b57cf86aaa918d899d4d to your computer and use it in GitHub Desktop.
Save Ilya-Gazman/b57cf86aaa918d899d4d to your computer and use it in GitHub Desktop.
An easy way to work with HTML in Android. Just set the textView with the build result.
public class HtmlBuilder {
public static final String COLOR = "[color]";
public enum Type{
BOLD("strong"),
ITALIC("em"),
COLOR("font color=\"#"+ HtmlBuilder.COLOR + "\""),
SMALL("small")
;
public final String stringType;
Type(String stringType){
this.stringType = stringType;
}
@Override
public String toString() {
return stringType;
}
}
private StringBuilder data = new StringBuilder();
public HtmlBuilder openColor(String color){
data.append("<").append(Type.COLOR.toString().replace(COLOR, color));
return this;
}
public HtmlBuilder append(String s) {
data.append(s);
return this;
}
public HtmlBuilder open(Type type){
data.append("<").append(type).append(">");
return this;
}
public HtmlBuilder close(Type type){
if(type == Type.COLOR){
data.append("</font>");
}
else {
data.append("</").append(type).append(">");
}
return this;
}
public Spanned build(){
return Html.fromHtml(data.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment