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:com.android.deskclock.Utils.java

/**
 * @param context - context used to get time format string resource
 * @param showAmPm - include the am/pm string if true
 * @return format string for 12 hours mode time
 *//* w  w w  . jav a2 s  . c  o m*/
public static CharSequence get12ModeFormat(Context context, boolean showAmPm) {
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "hma");
    if (!showAmPm) {
        pattern = pattern.replaceAll("a", "").trim();
    }

    // Replace spaces with "Hair Space"
    pattern = pattern.replaceAll(" ", "\u200A");
    // Build a spannable so that the am/pm will be formatted
    int amPmPos = pattern.indexOf('a');
    if (amPmPos == -1) {
        return pattern;
    }

    final Resources resources = context.getResources();
    final float amPmProportion = resources.getFraction(R.fraction.ampm_font_size_scale, 1, 1);
    final Spannable sp = new SpannableString(pattern);
    sp.setSpan(new RelativeSizeSpan(amPmProportion), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new StyleSpan(Typeface.NORMAL), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new TypefaceSpan("sans-serif"), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Make the font smaller for locales with long am/pm strings.
    if (Utils.isAmPmStringLong()) {
        final float proportion = resources.getFraction(R.fraction.reduced_clock_font_size_scale, 1, 1);
        sp.setSpan(new RelativeSizeSpan(proportion), 0, pattern.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return sp;
}

From source file:Main.java

public static CharSequence handleTextUrl(CharSequence content) {
    Matcher m = URL_PATTERN.matcher(content);

    Spannable spannable = null;
    while (m.find()) {
        // Ensure spannable
        if (spannable == null) {
            if (content instanceof Spannable) {
                spannable = (Spannable) content;
            } else {
                spannable = new SpannableString(content);
            }//w  w  w . j a v  a 2s . c  o m
        }

        int start = m.start();
        int end = m.end();

        URLSpan[] links = spannable.getSpans(start, end, URLSpan.class);
        if (links.length > 0) {
            // There has been URLSpan already, leave it alone
            continue;
        }

        URLSpan urlSpan = new URLSpan(m.group(0));
        spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return spannable == null ? content : spannable;
}

From source file:de.spiritcroc.modular_remote.Util.java

public static Spannable getContainerContentReadableName(Container container, @Nullable String prefix) {
    if (prefix == null) {
        prefix = "";
    }/*from w w  w .j a v a2s  . c o  m*/
    if (container.isEmpty()) {
        return new SpannableString(prefix + container.getReadableName());
    } else {
        ModuleFragment[] fragments = container.getFragments();
        String name = prefix + container.getReadableName();
        int spanStartIndex = name.length();
        name += " [" + fragments[0].getReadableName();
        int contentColor = fragments[0].getActivity().getResources().getColor(R.color.text_container_content);
        for (int i = 1; i < fragments.length; i++) {
            name += "; " + fragments[i].getReadableName();
        }
        name += "]";
        Spannable nameSpan = new SpannableString(name);
        nameSpan.setSpan(new ForegroundColorSpan(contentColor), spanStartIndex, name.length(),
                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return nameSpan;
    }
}

From source file:Main.java

public static CharSequence handleAcUrl(CharSequence content) {
    Matcher m = AC_PATTERN.matcher(content);

    Spannable spannable = null;
    while (m.find()) {
        // Ensure spannable
        if (spannable == null) {
            if (content instanceof Spannable) {
                spannable = (Spannable) content;
            } else {
                spannable = new SpannableString(content);
            }// w ww  .ja  v a  2  s  . c o  m
        }

        int start = m.start();
        int end = m.end();

        URLSpan[] links = spannable.getSpans(start, end, URLSpan.class);
        if (links.length > 0) {
            // There has been URLSpan already, leave it alone
            continue;
        }

        URLSpan urlSpan = new URLSpan("http://www.acfun.tv/v/" + m.group(0));
        spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return spannable == null ? content : spannable;
}

From source file:Main.java

public static boolean addEmoticons(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;/*from  w w  w .ja  va  2 s  .  c  om*/
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Map.Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;//w ww  .ja  v a2 s . co  m
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

/**
 * replace existing spannable with smiles
 * //  w w  w  . ja v a  2  s .  c om
 * @param context
 * @param spannable
 * @return
 */
public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:Main.java

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Map.Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class)) {
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end()) {
                    spannable.removeSpan(span);
                } else {
                    set = false;//from  w w w  . j  a v a 2 s . c  om
                    break;
                }
            }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

From source file:org.catnut.util.CatnutUtils.java

/**
 * textview/*from   ww w  .j  a v a  2 s  . c  om*/
 *
 * @param textView
 */
public static void removeLinkUnderline(TextView textView) {
    Spannable s = Spannable.Factory.getInstance().newSpannable(textView.getText());
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new TweetURLSpan(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:com.bt.heliniumstudentapp.MainActivity.java

protected static void setToolbarTitle(AppCompatActivity context, String title, String subtitle) {
    final Spannable toolbarTitle = new SpannableString(title);
    toolbarTitle.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, primaryTextColor)), 0,
            toolbarTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    try {/*from  ww w. j  av  a  2 s .  c  om*/
        context.getSupportActionBar().setTitle(toolbarTitle);
    } catch (NullPointerException e) {
        return;
    }

    if (subtitle != null) {
        final Spannable toolbarSubtitle = new SpannableString(subtitle);
        toolbarSubtitle.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, secondaryTextColor)), 0,
                toolbarSubtitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        context.getSupportActionBar().setSubtitle(toolbarSubtitle);
    }
}