Example usage for android.text SpannableString charAt

List of usage examples for android.text SpannableString charAt

Introduction

In this page you can find the example usage for android.text SpannableString charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:com.atwal.wakeup.battery.util.Utilities.java

public static SpannableString highlightKeywords(int color, String text, String keywords,
        boolean actionFirstMatch) {

    if (text != null && keywords != null && text.trim().length() == keywords.trim().length()) {
        return SpannableString.valueOf(text.trim());
    }//from ww  w  . j  a  v  a2 s .  co m

    SpannableString s = new SpannableString(text);
    Pattern p = Pattern.compile(keywords, Pattern.LITERAL);
    Matcher m = p.matcher(s);
    while (m.find()) {
        int end = m.end();
        try {
            if (s.charAt(end) != ' ') {
                s.setSpan(new UnderlineSpan(), end, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                s.setSpan(new UnderlineSpan(), end + 1, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (actionFirstMatch) {
            break;
        }
    }
    return s;
}