Change Spannable color - Android Graphics

Android examples for Graphics:Spannable Color

Description

Change Spannable color

Demo Code


import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;

public class Main{
    public static void color(@NonNull Spannable spannableText,
            @NonNull String textToHighlight, int color) {
        String text = spannableText.toString().toLowerCase();
        int begin = text.indexOf(textToHighlight.toLowerCase());
        while (begin >= 0) {
            int end = begin + textToHighlight.length();
            spannableText.setSpan(new ForegroundColorSpan(color), begin,
                    end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            begin = text.indexOf(textToHighlight.toLowerCase(), begin
                    + textToHighlight.length());
        }/*from w w  w.  j av  a 2s .  c  om*/
    }
}

Related Tutorials