Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.Color; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import java.util.ArrayList; public class Main { public static final int FLAG_TAG_BR = 1; public static final int FLAG_TAG_BOLD = 2; public static final int FLAG_TAG_COLOR = 4; public static final int FLAG_TAG_ALL = FLAG_TAG_BR | FLAG_TAG_BOLD | FLAG_TAG_COLOR; public static SpannableStringBuilder replaceTags(String str) { return replaceTags(str, FLAG_TAG_ALL); } public static SpannableStringBuilder replaceTags(String str, int flag) { try { int start; int end; StringBuilder stringBuilder = new StringBuilder(str); if ((flag & FLAG_TAG_BR) != 0) { while ((start = stringBuilder.indexOf("<br>")) != -1) { stringBuilder.replace(start, start + 4, "\n"); } while ((start = stringBuilder.indexOf("<br/>")) != -1) { stringBuilder.replace(start, start + 5, "\n"); } } ArrayList<Integer> bolds = new ArrayList<>(); if ((flag & FLAG_TAG_BOLD) != 0) { while ((start = stringBuilder.indexOf("<b>")) != -1) { stringBuilder.replace(start, start + 3, ""); end = stringBuilder.indexOf("</b>"); if (end == -1) { end = stringBuilder.indexOf("<b>"); } stringBuilder.replace(end, end + 4, ""); bolds.add(start); bolds.add(end); } } ArrayList<Integer> colors = new ArrayList<>(); if ((flag & FLAG_TAG_COLOR) != 0) { while ((start = stringBuilder.indexOf("<c#")) != -1) { stringBuilder.replace(start, start + 2, ""); end = stringBuilder.indexOf(">", start); int color = Color.parseColor(stringBuilder.substring(start, end)); stringBuilder.replace(start, end + 1, ""); end = stringBuilder.indexOf("</c>"); stringBuilder.replace(end, end + 4, ""); colors.add(start); colors.add(end); colors.add(color); } } SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder); // for (int a = 0; a < bolds.size() / 2; a++) { // spannableStringBuilder.setSpan( // new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), // bolds.get(a * 2), bolds.get(a * 2 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // } for (int a = 0; a < colors.size() / 3; a++) { spannableStringBuilder.setSpan(new ForegroundColorSpan(colors.get(a * 3 + 2)), colors.get(a * 3), colors.get(a * 3 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return spannableStringBuilder; } catch (Exception e) { e.printStackTrace(); } return new SpannableStringBuilder(str); } }