Search in sources :

Example 56 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project androidannotations by androidannotations.

the class TextWatchedActivityTest method testAfterTextChangeEditablePassed.

@Test
public void testAfterTextChangeEditablePassed() {
    assertThat(activity.afterEditable).isNull();
    TextView textView = (TextView) activity.findViewById(R.id.helloTextView);
    Editable s = new SpannableStringBuilder("hello");
    afterTextChanged(textView, s);
    assertThat(activity.afterEditable).isEqualTo(s);
}
Also used : Editable(android.text.Editable) TextView(android.widget.TextView) ShadowTextView(org.robolectric.shadows.ShadowTextView) SpannableStringBuilder(android.text.SpannableStringBuilder) Test(org.junit.Test)

Example 57 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project platform_frameworks_base by android.

the class NotificationBuilderTest method subst.

private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
    int i = 0;
    SpannableStringBuilder edit = new SpannableStringBuilder(in);
    while (i < edit.length()) {
        if (edit.charAt(i) == ch) {
            edit.replace(i, i + 1, sub);
            i += sub.length();
        } else {
            i++;
        }
    }
    return edit;
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 58 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project platform_frameworks_base by android.

the class NotificationTests method BOLD.

static SpannableStringBuilder BOLD(CharSequence str) {
    final SpannableStringBuilder ssb = new SpannableStringBuilder(str);
    ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, ssb.length(), 0);
    return ssb;
}
Also used : StyleSpan(android.text.style.StyleSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 59 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project platform_frameworks_base by android.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 60 with SpannableStringBuilder

use of android.text.SpannableStringBuilder in project platform_frameworks_base by android.

the class DigitsKeyListener method filter.

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    CharSequence out = super.filter(source, start, end, dest, dstart, dend);
    if (mSign == false && mDecimal == false) {
        return out;
    }
    if (out != null) {
        source = out;
        start = 0;
        end = out.length();
    }
    int sign = -1;
    int decimal = -1;
    int dlen = dest.length();
    for (int i = 0; i < dstart; i++) {
        char c = dest.charAt(i);
        if (isSignChar(c)) {
            sign = i;
        } else if (isDecimalPointChar(c)) {
            decimal = i;
        }
    }
    for (int i = dend; i < dlen; i++) {
        char c = dest.charAt(i);
        if (isSignChar(c)) {
            // Nothing can be inserted in front of a sign character.
            return "";
        } else if (isDecimalPointChar(c)) {
            decimal = i;
        }
    }
    /*
         * If it does, we must strip them out from the source.
         * In addition, a sign character must be the very first character,
         * and nothing can be inserted before an existing sign character.
         * Go in reverse order so the offsets are stable.
         */
    SpannableStringBuilder stripped = null;
    for (int i = end - 1; i >= start; i--) {
        char c = source.charAt(i);
        boolean strip = false;
        if (isSignChar(c)) {
            if (i != start || dstart != 0) {
                strip = true;
            } else if (sign >= 0) {
                strip = true;
            } else {
                sign = i;
            }
        } else if (isDecimalPointChar(c)) {
            if (decimal >= 0) {
                strip = true;
            } else {
                decimal = i;
            }
        }
        if (strip) {
            if (end == start + 1) {
                // Only one character, and it was stripped.
                return "";
            }
            if (stripped == null) {
                stripped = new SpannableStringBuilder(source, start, end);
            }
            stripped.delete(i - start, i + 1 - start);
        }
    }
    if (stripped != null) {
        return stripped;
    } else if (out != null) {
        return out;
    } else {
        return null;
    }
}
Also used : SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SpannableStringBuilder (android.text.SpannableStringBuilder)322 ForegroundColorSpan (android.text.style.ForegroundColorSpan)55 ImageSpan (android.text.style.ImageSpan)52 TextView (android.widget.TextView)35 StyleSpan (android.text.style.StyleSpan)33 View (android.view.View)33 Spanned (android.text.Spanned)27 Drawable (android.graphics.drawable.Drawable)26 Spannable (android.text.Spannable)24 SpannableString (android.text.SpannableString)21 Test (org.junit.Test)21 CharacterStyle (android.text.style.CharacterStyle)18 TextPaint (android.text.TextPaint)17 RelativeSizeSpan (android.text.style.RelativeSizeSpan)17 Paint (android.graphics.Paint)16 TextAppearanceSpan (android.text.style.TextAppearanceSpan)15 ImageView (android.widget.ImageView)14 Intent (android.content.Intent)13 Editable (android.text.Editable)13 Context (android.content.Context)12