Example usage for android.text SpannableStringBuilder setSpan

List of usage examples for android.text SpannableStringBuilder setSpan

Introduction

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

Prototype

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

Source Link

Document

Mark the specified range of text with the specified object.

Usage

From source file:Main.java

public static SpannableStringBuilder applyBoldStyle(String text) {
    SpannableStringBuilder ss = new SpannableStringBuilder(text);
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return ss;/*ww  w. jav  a  2s  .  co  m*/
}

From source file:Main.java

public static CharSequence getError(String msg) {
    int color = Color.WHITE;
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
    SpannableStringBuilder string = new SpannableStringBuilder(msg);
    string.setSpan(foregroundColorSpan, 0, msg.length(), 0);
    return string;
}

From source file:Main.java

public static SpannableStringBuilder getFrontTextStyle(String frontText, String afterText, int frontColor,
        int frontTextSize) {
    StringBuilder sb = new StringBuilder();
    sb.append("/").append(afterText);
    int len = sb.length();
    sb.insert(0, frontText);/*from  w w w.  j av  a2 s.c om*/
    SpannableStringBuilder style = new SpannableStringBuilder(sb);
    style.setSpan(new ForegroundColorSpan(frontColor), 0, sb.length() - len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    style.setSpan(new AbsoluteSizeSpan(frontTextSize), 0, sb.length() - len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return style;
}

From source file:Main.java

/**
 * Set an error message to a text view/* ww  w .  ja v  a 2  s .  co m*/
 *
 * @param color   Set the color foreground for the span
 * @param message Message to be shown
 * @param txtView Text View to which the message will be added
 */
public static void setError(int color, String message, TextView txtView) {
    ForegroundColorSpan fgcspan = new ForegroundColorSpan(color);
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(message);
    ssbuilder.setSpan(fgcspan, 0, message.length(), 0);
    txtView.setError(ssbuilder);
}

From source file:Main.java

public static CharSequence convertStringToShowErrorInEditText(String string) {
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.WHITE);
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(string);
    spannableStringBuilder.setSpan(foregroundColorSpan, 0, string.length(), 0);
    return spannableStringBuilder;
}

From source file:Main.java

public static void makeTextViewHyperlink(TextView tv) {
    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(tv.getText());/* w  ww . j a v  a 2s .com*/
    ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(ssb, TextView.BufferType.SPANNABLE);
}

From source file:Main.java

/**
 * Given either a Spannable String or a regular String and a token, apply
 * the given CharacterStyle to the span between the tokens, and also remove
 * tokens.//from   www . j  av  a 2  s .  c  o  m
 * <p/>
 * For example, {@code setSpanBetweenTokens("Hello ##world##!", "##",
 *new ForegroundColorSpan(0xFFFF0000));} will return a CharSequence
 * {@code "Hello world!"} with {@code world} in red.
 *
 * @param text  The text, with the tokens, to adjust.
 * @param token The token string; there should be at least two instances of
 *              token in text.
 * @param cs    The style to apply to the CharSequence. WARNING: You cannot
 *              send the same two instances of this parameter, otherwise the
 *              second call will remove the original span.
 * @return A Spannable CharSequence with the new style applied.
 * @see <a href="http://developer.android.com/reference/android/text/style/CharacterStyle
 * .html">Character Style</a>
 */
public static CharSequence setSpanBetweenTokens(CharSequence text, String token, CharacterStyle... cs) {
    // Start and end refer to the points where the span will apply
    int tokenLen = token.length();
    int start = text.toString().indexOf(token) + tokenLen;
    int end = text.toString().indexOf(token, start);

    if (start > -1 && end > -1) {
        // Copy the spannable string to a mutable spannable string
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        for (CharacterStyle c : cs) {
            ssb.setSpan(c, start, end, 0);
        }

        // Delete the tokens before and after the span
        ssb.delete(end, end + tokenLen);
        ssb.delete(start - tokenLen, start);

        text = ssb;
    }

    return text;
}

From source file:Main.java

public static void appendSpannable(SpannableStringBuilder builder, String text, Object[] styles) {
    final int from = builder.length();
    builder.append(text);/*from w w w.j  a  v  a 2 s .co  m*/
    final int to = builder.length();

    for (Object style : styles) {
        builder.setSpan(style, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:Main.java

public static Spannable replaceTags(String str, Context context) {
    try {//from   w ww  .j  a  va 2 s  .  c  om
        int start = -1;
        int startColor = -1;
        int end = -1;
        StringBuilder stringBuilder = new StringBuilder(str);
        while ((start = stringBuilder.indexOf("<br>")) != -1) {
            stringBuilder.replace(start, start + 4, "\n");
        }
        while ((start = stringBuilder.indexOf("<br/>")) != -1) {
            stringBuilder.replace(start, start + 5, "\n");
        }
        ArrayList<Integer> bolds = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        while ((start = stringBuilder.indexOf("<b>")) != -1
                || (startColor = stringBuilder.indexOf("<c")) != -1) {
            if (start != -1) {
                stringBuilder.replace(start, start + 3, "");
                end = stringBuilder.indexOf("</b>");
                stringBuilder.replace(end, end + 4, "");
                bolds.add(start);
                bolds.add(end);
            } else if (startColor != -1) {
                stringBuilder.replace(startColor, startColor + 2, "");
                end = stringBuilder.indexOf(">", startColor);
                int color = Color.parseColor(stringBuilder.substring(startColor, end));
                stringBuilder.replace(startColor, end + 1, "");
                end = stringBuilder.indexOf("</c>");
                stringBuilder.replace(end, end + 4, "");
                colors.add(startColor);
                colors.add(end);
                colors.add(color);
            }
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(stringBuilder);
        for (int a = 0; a < colors.size() / 3; a++) {
            spannableStringBuilder.setSpan(new ForegroundColorSpan(colors.get(a * 3 + 2)), colors.get(a * 3),
                    colors.get(a * 3 + 1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } catch (Exception e) {

    }
    return new SpannableStringBuilder(str);
}

From source file:Main.java

public static CharSequence addImageToText(Context context, CharSequence text, int resID, int start, int end) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    Drawable d = context.getResources().getDrawable(resID);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    builder.setSpan(span, text.length(), 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    return builder;
}