replace High Light Tag in SpannableStringBuilder - Android android.text

Android examples for android.text:SpannableString

Description

replace High Light Tag in SpannableStringBuilder

Demo Code

import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.Patterns;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static void replaceHighLightTag(
            SpannableStringBuilder spannableStringBuilder, Pattern patten)
            throws Exception {
        Matcher matcher = patten.matcher(spannableStringBuilder);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            int spannableStringBuilderLenght = spannableStringBuilder
                    .length();/*from  w w w . j a v a2s.  c  om*/
            String matcherStr = matcher.group();
            String replaceResultStr = matcherStr.substring(
                    "<highlight>".length(), matcherStr.length()
                            - "</highlight>".length());
            spannableStringBuilder.replace(start, end, replaceResultStr);
            if (end < spannableStringBuilderLenght) {
                replaceHighLightTag(spannableStringBuilder, patten);
            }
        }
    }

}

Related Tutorials