Example usage for android.text SpannableString setSpan

List of usage examples for android.text SpannableString setSpan

Introduction

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

Prototype

public void setSpan(Object what, int start, int end, int flags) 

Source Link

Usage

From source file:nl.mpcjanssen.simpletask.util.Util.java

public static void setColor(SpannableString ss, int color) {

    ss.setSpan(new ForegroundColorSpan(color), 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:Main.java

public static SpannableString replaceImageSpan(CharSequence charSequence, String regPattern,
        Drawable drawable) {//from w  w  w.j av a  2s . c  o  m
    SpannableString ss = charSequence instanceof SpannableString ? (SpannableString) charSequence
            : new SpannableString(charSequence);
    try {
        ImageSpan is = new ImageSpan(drawable);
        Pattern pattern = Pattern.compile(regPattern);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            String key = matcher.group();
            ss.setSpan(is, matcher.start(), matcher.start() + key.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ss;
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void appendPushNotificationState(Context context, MasterSecret masterSecret,
        NotificationState notificationState, Cursor cursor) {
    if (masterSecret != null)
        return;/* w  w  w  .jav a  2s  .c  o  m*/

    PushDatabase.Reader reader = null;
    IncomingPushMessage message;

    try {
        reader = DatabaseFactory.getPushDatabase(context).readerFor(cursor);

        while ((message = reader.getNext()) != null) {
            Recipient recipient;

            try {
                recipient = RecipientFactory.getRecipientsFromString(context, message.getSource(), false)
                        .getPrimaryRecipient();
            } catch (RecipientFormattingException e) {
                Log.w("MessageNotifier", e);
                recipient = Recipient.getUnknownRecipient(context);
            }

            Recipients recipients = RecipientFactory.getRecipientsFromMessage(context, message, false);
            long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
            SpannableString body = new SpannableString(
                    context.getString(R.string.MessageNotifier_encrypted_message));
            body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            notificationState
                    .addNotification(new NotificationItem(recipient, recipients, null, threadId, body, null));
        }
    } finally {
        if (reader != null)
            reader.close();
    }
}

From source file:Main.java

public static SpannableString colourize(String raw) {
    String uncoloredName = uncolourize(raw);
    SpannableString str = new SpannableString(uncoloredName);
    char c, cc;//w w w . j a  v  a 2s.  co m
    String color = "RESETT";
    int start = 0;
    int end = 0;
    for (int i = 0; i < raw.length() - 1; i++) {
        c = raw.charAt(i);
        cc = raw.charAt(i + 1);
        // if we detect a new color tag
        if (c == '0' && cc == 'x') {
            if (start != end) {
                str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, end,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            start = end;
            i += 2;
            color = "";
            for (int k = i; k < i + 6; k++) {
                try {
                    color += raw.charAt(k);
                } catch (IndexOutOfBoundsException e) {
                    color = "";
                    break;
                }
            }
            i += 5;
        } else {
            end++;
        }
    }
    str.setSpan(new ForegroundColorSpan(getColorInt(color)), start, uncoloredName.length(),
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return str;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }//  www.  j a  v  a 2  s .  c  o m
    }
    return spannableString;
}

From source file:org.kontalk.ui.ComposeMessage.java

static CharSequence applyUpdatingStyle(CharSequence text) {
    // we call toString() to strip any existing span
    SpannableString status = new SpannableString(text.toString());
    status.setSpan(sUpdatingTextSpan, 0, status.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return status;
}

From source file:Main.java

public static CharSequence replace(Context ctx, String text) {
    if (text == null) {
        return null;
    }//from  w  w w.ja v a2s  .  com
    SpannableString spannableString = new SpannableString(text);
    Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        String factText = matcher.group();
        String key = factText.substring(1);
        if (emotionTexts.contains(factText)) {
            Bitmap bitmap = getDrawableByName(ctx, key);
            ImageSpan image = new ImageSpan(ctx, bitmap);
            int start = matcher.start();
            int end = matcher.end();
            spannableString.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return spannableString;
}

From source file:com.csipsimple.service.SipNotifications.java

protected static CharSequence buildTickerMessage(Context context, String address, String body) {
    String displayAddress = address;

    StringBuilder buf = new StringBuilder(
            displayAddress == null ? "" : displayAddress.replace('\n', ' ').replace('\r', ' '));
    buf.append(':').append(' ');

    int offset = buf.length();

    if (!TextUtils.isEmpty(body)) {
        body = body.replace('\n', ' ').replace('\r', ' ');
        buf.append(body);/*w ww.  j  a va2  s . c  om*/
    }

    SpannableString spanText = new SpannableString(buf.toString());
    spanText.setSpan(new StyleSpan(Typeface.BOLD), 0, offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spanText;
}

From source file:com.android.mail.browse.SendersView.java

public static SpannableString getFormattedToHeader() {
    final SpannableString formattedToHeader = new SpannableString(sToHeaderString);
    final CharacterStyle readStyle = CharacterStyle.wrap(sReadStyleSpan);
    formattedToHeader.setSpan(readStyle, 0, formattedToHeader.length(), 0);
    return formattedToHeader;
}

From source file:Main.java

public static void setSpanBetweenTokens(SpannableString ss, String text, String token, CharacterStyle... cs) {
    // Start and end refer to the points where the span will apply
    int tokenLen = token.length();
    int base = 0;

    while (true) {
        int start = text.indexOf(token, base);
        int end = text.indexOf(token, start + tokenLen);

        if (start < 0 || end < 0) {
            break;
        }/*  www .  ja v a  2 s  .co  m*/

        base = end + tokenLen;

        for (CharacterStyle c : cs) {
            ss.setSpan(CharacterStyle.wrap(c), start + tokenLen, end, 0);
        }

        Drawable blankDrawable = new Drawable() {

            @Override
            public void setColorFilter(ColorFilter cf) {
            }

            @Override
            public void setAlpha(int alpha) {
            }

            @Override
            public int getOpacity() {
                return 0;
            }

            @Override
            public void draw(Canvas canvas) {
            }
        };

        // because AbsoluteSizeSpan(0) doesn't work on older versions
        ss.setSpan(new ImageSpan(blankDrawable), start, start + tokenLen, 0);
        ss.setSpan(new ImageSpan(blankDrawable), end, end + tokenLen, 0);
    }
}