Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

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

Prototype

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

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:Main.java

public static void setPartialColor(TextView tv, int start, int end, int textColor) {
    String s = tv.getText().toString();
    Spannable spannable = new SpannableString(s);
    spannable.setSpan(new ForegroundColorSpan(textColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(spannable);/*from w w w  .j  a va  2  s .  c o  m*/
}

From source file:Main.java

public static void setPartialSizeAndColor(TextView tv, int start, int end, int textSize, int textColor) {
    String s = tv.getText().toString();
    Spannable spannable = new SpannableString(s);
    spannable.setSpan(new AbsoluteSizeSpan(textSize, false), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new ForegroundColorSpan(textColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(spannable);/*from w  w w  . ja va2  s .  c  om*/
}

From source file:Main.java

/**
 * Returns a CharSequence containing a bulleted and properly indented list.
 *
 * @param leadingMargin/*from w w w . ja va 2  s .c  o  m*/
 *            In pixels, the space between the left edge of the bullet and
 *            the left edge of the text.
 * @param lines
 *            An array of CharSequences. Each CharSequences will be a
 *            separate line/bullet-point.
 * @return
 */
public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < lines.length; i++) {
        CharSequence line = lines[i] + (i < lines.length - 1 ? "\n" : "");
        Spannable spannable = new SpannableString(line);
        spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        sb.append(spannable);
    }
    return sb;
}

From source file:Main.java

/**
 * Iterates over an array of tags and applies them to the beginning of the specified
 * Spannable object so that future text appended to the text will have the styling
 * applied to it. Do not call this method directly.
 *///w ww .  ja  v  a  2s .  c  om
private static void openTags(Spannable text, Object[] tags) {
    for (Object tag : tags) {
        text.setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK);
    }
}

From source file:Main.java

/**
 * Apply bold span to a spannable string.
 *///from ww  w.  j a v a2s  .  c  o  m
public static void applyBoldSpan(Spannable spannable, int startIndex, int endIndex) {

    StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD);

    spannable.setSpan(bold, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:Main.java

@NonNull
public static Spannable setSpans(@NonNull Spannable spanned, int start, int end, int flags,
        @NonNull Object... spans) {
    for (Object span : spans) {
        spanned.setSpan(span, start, end, flags);
    }//from ww  w .j a  va2 s .  c  om
    return spanned;
}

From source file:Main.java

public static void setTextViewCharHilighted(TextView textView, String text, int startIndex, int endIndex,
        int color) {
    if (textView == null || text == null) {
        return;//from  w  ww  .java2  s . co m
    }

    if (startIndex < 0) {
        return;
    }

    if (endIndex > text.length()) {
        return;
    }

    textView.setText(text, BufferType.SPANNABLE);

    Spannable span = (Spannable) textView.getText();
    if (span == null) {
        return;
    }

    span.setSpan(new ForegroundColorSpan(color), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.googlecode.eyesfree.brailleback.DisplaySpans.java

/**
 * Marks a region of {@code spanned} as having focus.
 *//*w w w . ja  v a  2s . c o m*/
public static void addFocus(Spannable spannable, int start, int end) {
    spannable.setSpan(new FocusSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:Main.java

/**
 * Returns the bulleted list based on the given {@code lines}.
 * If one of lines starts with {@link BulletListUtil#BAD_FIRST_SYMBOLS}, then such symbol is
 * removed from there (sometimes bad lines are received from the Food2Work).
 * Also if line consists of the upper case words, then this line is used like a header and is
 * underlined./*from   ww w .  ja v a 2 s . c om*/
 *
 * @param leadingMargin In pixels, the space between the left edge of the bullet and the left
 *                         edge of the text
 * @param lines List of strings. Each string will be a separate item in the bulleted list
 * @return The bulleted list based on the given {@code lines}
 */
public static CharSequence makeBulletList(List<String> lines, int leadingMargin) {
    List<Spanned> spanned = new ArrayList<>(lines.size());
    for (String line : lines) {
        if (!line.trim().isEmpty()) {
            Spanned spannedLine = Html.fromHtml(removeBadFirstCharacters(line.trim()));
            spanned.add(spannedLine);
        }
    }

    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (int i = 0; i < spanned.size(); i++) {
        CharSequence line = spanned.get(i) + (i < spanned.size() - 1 ? "\n" : "");
        boolean underlineNeeded = isUpperCase(line);
        Spannable spannable = new SpannableString(line);
        if (underlineNeeded) {
            spannable.setSpan(new UnderlineSpan(), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } else {
            spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(),
                    Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        sb.append(spannable);
    }
    return sb;
}

From source file:Main.java

/**
 * "Closes" the specified tags on a Spannable by updating the spans to be
 * endpoint-exclusive so that future text appended to the end will not take
 * on the same styling. Do not call this method directly.
 *//*  w w w  . ja va 2s  . c o m*/
private static void closeTags(Spannable text, Object[] tags) {
    int len = text.length();
    for (Object tag : tags) {
        if (len > 0) {
            text.setSpan(tag, 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            text.removeSpan(tag);
        }
    }
}