set Texts Color By Number for TextView - Android android.widget

Android examples for android.widget:TextView

Description

set Texts Color By Number for TextView

Demo Code

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Main{


    public static void setTextsColorByNum(TextView textView, String text,
            String color_text, int color, float size) {

        if (isNull(text))
            return;
        SpannableStringBuilder styledText = new SpannableStringBuilder(text);

        int index = text.lastIndexOf(color_text);
        int end = index + color_text.length();
        if (color != 0) {
            styledText.setSpan(new ForegroundColorSpan(color), index, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//from   www .  ja v a  2  s  . com
        if (size > 0) {

            styledText.setSpan(new RelativeSizeSpan(size), index, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setText(styledText);
    }

}

Related Tutorials