Last active
February 19, 2018 21:06
-
-
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.
This file contains hidden or 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 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