get Colored Word as SpannableString - Android Graphics

Android examples for Graphics:Spannable Color

Description

get Colored Word as SpannableString

Demo Code


//package com.java2s;

import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

public class Main {
    public static CharSequence getColoredWord(int[] colors, String word) {
        SpannableString spannableString = new SpannableString(word);
        for (int i = 0; i < word.length(); ++i) {
            setLetterColor(spannableString, i, colors[i]);
        }/*from ww w  . j  a v  a2  s. c  o m*/
        return spannableString;
    }

    private static void setLetterColor(SpannableString string,
            int position, int color) {
        string.setSpan(new ForegroundColorSpan(color), position,
                position + 1, 0);
    }
}

Related Tutorials