Java tutorial
//package com.java2s; /* * This is the source code of Telegram for Android v. 1.4.x. * It is licensed under GNU GPL v. 2 or later. * You should have received a copy of the license in this archive (see LICENSE). * * Copyright Nikolai Kudashov, 2013-2014. */ import android.content.Context; import android.graphics.Color; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import java.util.ArrayList; public class Main { public static Spannable replaceTags(String str, Context context) { try { int start = -1; int startColor = -1; int end = -1; StringBuilder stringBuilder = new StringBuilder(str); 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<>(); ArrayList<Integer> colors = new ArrayList<>(); while ((start = stringBuilder.indexOf("<b>")) != -1 || (startColor = stringBuilder.indexOf("<c")) != -1) { if (start != -1) { stringBuilder.replace(start, start + 3, ""); end = stringBuilder.indexOf("</b>"); stringBuilder.replace(end, end + 4, ""); bolds.add(start); bolds.add(end); } else if (startColor != -1) { stringBuilder.replace(startColor, startColor + 2, ""); end = stringBuilder.indexOf(">", startColor); int color = Color.parseColor(stringBuilder.substring(startColor, end)); stringBuilder.replace(startColor, end + 1, ""); end = stringBuilder.indexOf("</c>"); stringBuilder.replace(end, end + 4, ""); colors.add(startColor); colors.add(end); colors.add(color); } } SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder); 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) { } return new SpannableStringBuilder(str); } }