Example usage for android.text Spannable getSpans

List of usage examples for android.text Spannable getSpans

Introduction

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

Prototype

public <T> T[] getSpans(int start, int end, Class<T> type);

Source Link

Document

Return an array of the markup objects attached to the specified slice of this CharSequence and whose type is the specified type or a subclass of it.

Usage

From source file:Main.java

/**
 * Remove all spans./*w w  w  . j  a v  a2  s.  c  o  m*/
 */
public static void removeSpans(Spannable spannable) {

    Object spansToRemove[] = spannable.getSpans(0, spannable.length(), Object.class);

    for (Object span : spansToRemove) {

        if (span instanceof CharacterStyle) {

            spannable.removeSpan(span);
        }
    }
}

From source file:Main.java

public static void setClickable(final TextView textView) {
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    Spannable sp = (Spannable) textView.getText();
    ImageSpan[] images = sp.getSpans(0, textView.getText().length(), ImageSpan.class);

    for (ImageSpan span : images) {
        final String image_src = span.getSource();
        final int start = sp.getSpanStart(span);
        final int end = sp.getSpanEnd(span);

        ClickableSpan click_span = new ClickableSpan() {
            @Override//  ww  w .  j  av  a 2s .  c  o  m
            public void onClick(View widget) {
                String[] strs = image_src.split("/");
                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LilyClient/"
                        + strs[strs.length - 2] + "-" + strs[strs.length - 1];

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setType("image/*");
                intent.setDataAndType(Uri.fromFile(new File(filePath)), "image/*");
                textView.getContext().startActivity(intent);

            }
        };
        ClickableSpan[] click_spans = sp.getSpans(start, end, ClickableSpan.class);
        if (click_spans.length != 0) {
            for (ClickableSpan c_span : click_spans) {
                sp.removeSpan(c_span);
            }
        }
        sp.setSpan(click_span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:Main.java

public static Spanned toSpanned(final Context context, final String toastString,
        @ColorRes final int toastColor) {
    final String string = "" + context.getResources().getColor(toastColor);
    final Spannable spannable = (Spannable) Html
            .fromHtml(colorize_a(colorize_em(toastString, string, true), string));
    for (final URLSpan urlSpan : (URLSpan[]) spannable.getSpans(0, spannable.length(), (Class) URLSpan.class)) {
        spannable.setSpan(urlSpan, spannable.getSpanStart(urlSpan), spannable.getSpanEnd(urlSpan), 0);
    }/*from   ww w.  jav  a2  s  .  c om*/
    return spannable;
}

From source file:rosmi.acagild.alarmclock.utilities.GeneralUtilities.java

public static void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) 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);/*from  w  ww  .  j a va2s . c om*/
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

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

/**
 * Recycles objects owned by the spannable.  In particular, any
 * accessibility nodes that have been associated with {@code spannable}
 * are recycled and removed./*from   ww w. j a v  a2s .c om*/
 */
public static void recycleSpans(CharSequence chars) {
    if (!(chars instanceof Spannable)) {
        return;
    }
    Spannable spannable = (Spannable) chars;
    AccessibilityNodeInfoCompat[] nodes = spannable.getSpans(0, spannable.length(),
            AccessibilityNodeInfoCompat.class);
    for (AccessibilityNodeInfoCompat node : nodes) {
        node.recycle();
        spannable.removeSpan(node);
    }
}

From source file:com.microsoft.mimickeralarm.utilities.GeneralUtilities.java

public static void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) 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);//from  w  w  w . j  av  a 2s  .com
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:cn.dreamtobe.emoji.ellipsize.helper.SpanEllipsizeEndHelper.java

private static void removeClickableSpan(Spannable content) {
    ClickableSpan[] clickSpans = content.getSpans(0, content.length(), ClickableSpan.class);
    for (int i = 0; i < clickSpans.length; i++) {
        content.removeSpan(clickSpans[i]);
    }// w w w  .  j a va2s. com
}

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 ww . j a v  a  2 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

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 . j  a  v a  2 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

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 w  w .  j a  va2s .co  m*/
                    break;
                }
            }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}