set Word Typed Face for TextView - Android User Interface

Android examples for User Interface:TextView

Description

set Word Typed Face for TextView

Demo Code


//package com.java2s;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;

import android.text.style.StyleSpan;
import android.widget.TextView;

public class Main {

    public static void setWordTypedFace(TextView view, int style,
            Object... words) {/*from  ww  w . j  a  v a 2 s.  c o m*/
        CharSequence info = view.getText();
        if (!TextUtils.isEmpty(info) && null != words) {
            SpannableStringBuilder span = new SpannableStringBuilder(info);
            for (int i = 0; i < words.length; i++) {
                if (null != words[i]) {
                    int start = info.toString()
                            .indexOf(words[i].toString());
                    if (-1 != start) {
                        span.setSpan(new StyleSpan(style), start, start
                                + words[i].toString().length(),
                                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    }
                }
            }
            view.setText(span);
        }
    }
}

Related Tutorials