Example usage for android.text SpannableStringBuilder SpannableStringBuilder

List of usage examples for android.text SpannableStringBuilder SpannableStringBuilder

Introduction

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

Prototype

public SpannableStringBuilder(CharSequence text) 

Source Link

Document

Create a new SpannableStringBuilder containing a copy of the specified text, including its spans if any.

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;/*  w ww  .  j  a va 2s. co  m*/
}

From source file:Main.java

public static CharSequence htmlfrom(CharSequence text) {
    Pattern htmlflag1 = Pattern.compile("<(.*?)>");
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    Matcher matcher = htmlflag1.matcher(text);
    while (matcher.find()) {
        builder.delete(matcher.start(), matcher.end());
        text = builder;//from   w  w w  .  j  a v a  2s .c o m
        matcher = htmlflag1.matcher(text);
    }
    Pattern htmlflag2 = Pattern.compile("&(.*?);");
    matcher = htmlflag2.matcher(text);
    while (matcher.find()) {
        builder.delete(matcher.start(), matcher.end());
        text = builder;
        matcher = htmlflag2.matcher(text);
    }

    return builder.toString();

}

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 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 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;
}

From source file:Main.java

/**
 * Set an error message to a text view/*from w  ww.j a  v a  2s.  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 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);/*w ww.  ja va  2s  . c  o m*/
    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

public static Spannable applyKerning(CharSequence src, float kerning, int start, int end) {
    if (src == null)
        return null;
    final int srcLength = src.length();
    if (srcLength < 2)
        return src instanceof Spannable ? (Spannable) src : new SpannableString(src);
    if (start < 0)
        start = 0;/* www  .  java  2s.c o m*/
    if (end > srcLength)
        end = srcLength;

    final String nonBreakingSpace = "\u00A0";
    final SpannableStringBuilder builder = src instanceof SpannableStringBuilder ? (SpannableStringBuilder) src
            : new SpannableStringBuilder(src);
    for (int i = src.length(); i >= 1; i--) {
        builder.insert(i, nonBreakingSpace);
        builder.setSpan(new ScaleXSpan(kerning), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return builder;
}

From source file:Main.java

public static SpannableStringBuilder getSpannableStringFromList(List<HashMap<String, Object>> list) {
    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    int position = 0;
    for (int i = 0; i < list.size(); i++) {
        HashMap<String, Object> map = list.get(i);
        try {/*from w  w  w . ja v  a2  s  .  c  o  m*/
            String st = (String) map.get(RICHTEXT_STRING);
            ssb.append(st);
            int len = st.length();
            if (map.containsKey(RICHTEXT_COLOR)) {
                int color = ((Integer) map.get(RICHTEXT_COLOR)).intValue();
                ssb.setSpan(new ForegroundColorSpan(color), position, position + len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (map.containsKey(RICHTEXT_SIZE)) {
                int size = ((Integer) map.get(RICHTEXT_SIZE)).intValue();
                ssb.setSpan(new AbsoluteSizeSpan(size), position, position + len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (map.containsKey(RICHTEXT_RSIZE)) {
                float size = ((Float) map.get(RICHTEXT_RSIZE)).floatValue();
                ssb.setSpan(new RelativeSizeSpan(size), position, position + len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (map.containsKey(RICHTEXT_DELETE)) {
                ssb.setSpan(new StrikethroughSpan(), position, position + len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            //              android.text.style.RelativeSizeSpan
            position = position + len;
        } catch (Exception e) {
            return null;
        }
    }
    return ssb;
}

From source file:Main.java

/**
 * Given a snippet string with matching segments surrounded by curly
 * braces, turn those areas into bold spans, removing the curly braces.
 *//*from  w ww.java2 s .c o  m*/
public static Spannable buildStyledSnippet(String snippet) {
    final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);

    // Walk through string, inserting bold snippet spans
    int startIndex, endIndex = -1, delta = 0;
    while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
        endIndex = snippet.indexOf('}', startIndex);

        // Remove braces from both sides
        builder.delete(startIndex - delta, startIndex - delta + 1);
        builder.delete(endIndex - delta - 1, endIndex - delta);

        // Insert bold style
        builder.setSpan(new StyleSpan(Typeface.BOLD), startIndex - delta, endIndex - delta - 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //builder.setSpan(new ForegroundColorSpan(0xff111111),
        //        startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        delta += 2;
    }

    return builder;
}