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

/**
 * replace existing spannable with smiles
 * /*from  w  w w  .j  av a  2s . c  o m*/
 * @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

/**
 * replace existing spannable with smiles
 * @param context/*from  w  w  w .  j  a v  a2s  .  co  m*/
 * @param spannable
 * @return
 */
public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Object> 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;
                Object value = entry.getValue();
                if (value instanceof String && !((String) value).startsWith("http")) {
                    File file = new File((String) value);
                    if (!file.exists() || file.isDirectory()) {
                        return false;
                    }
                    spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)), matcher.start(),
                            matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannable.setSpan(new ImageSpan(context, (Integer) value), matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
    }

    return hasChanges;
}

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);
            }//from w  w  w  .  j  a  v  a  2s.  co 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 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  ava 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(m.group(0));
        spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return spannable == null ? content : spannable;
}

From source file:com.silentcircle.common.util.ViewUtil.java

public static Intent createIntentForLinks(TextView view) {

    CharSequence text = view.getText();

    if (text instanceof Spannable) {

        Spannable stext = (Spannable) text;
        URLSpan[] spans = stext.getSpans(0, stext.length(), URLSpan.class);

        if (spans != null && spans.length > 0) {
            ChooserBuilder chooser = new ChooserBuilder(view.getContext());
            chooser.label(R.string.view);
            for (URLSpan span : spans) {
                String url = span.getURL();
                CharSequence label = stext.subSequence(stext.getSpanStart(span), stext.getSpanEnd(span));
                chooser.intent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), label);
            }/*from   w w  w.j  ava 2s .  c  om*/
            return chooser.build();

        }
    }
    return null;
}

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

/**
 * textview//  w w  w . j  a  v a 2 s . co m
 *
 * @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:org.onebusaway.android.util.UIUtils.java

public static void removeAllClickableSpans(TextView v) {
    Spannable text = (Spannable) v.getText();
    ClickableSpan[] spans = text.getSpans(0, text.length(), ClickableSpan.class);
    for (ClickableSpan cs : spans) {
        text.removeSpan(cs);// ww w  . j ava2s. c om
    }
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

private static void stripUrls(final Spannable text) {
    final URLSpan[] spans = text.getSpans(0, text.length(), URLSpan.class);
    for (final URLSpan span : spans) {
        text.removeSpan(span);//from  ww w . ja va 2  s  . c  o  m
    }
}

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

/**
 * Adds {@code node} as a span on {@code content} if not already
 * fully covered by an accessibility node info span.
 *///from w  w  w. j a va2s.  c o m
private void addNodeSpanForUncovered(AccessibilityNodeInfoCompat node, Spannable spannable) {
    AccessibilityNodeInfoCompat[] spans = spannable.getSpans(0, spannable.length(),
            AccessibilityNodeInfoCompat.class);
    for (AccessibilityNodeInfoCompat span : spans) {
        if (spannable.getSpanStart(span) == 0 && spannable.getSpanEnd(span) == spannable.length()) {
            return;
        }
    }
    DisplaySpans.setAccessibilityNode(spannable, node);
}

From source file:com.lloydtorres.stately.helpers.SparkleHelper.java

/**
 * Stylify text view to primary colour and no underline
 * @param c App context/*w  ww  .  j  a v a 2 s  .  co m*/
 * @param t TextView
 */
public static void styleLinkifiedTextView(Context c, TextView t) {
    // Get individual spans and replace them with clickable ones.
    Spannable s = new SpannableString(t.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 URLSpanNoUnderline(c, span.getURL());
        s.setSpan(span, start, end, 0);
    }

    t.setText(s);
    // Need to set this to allow for clickable TextView links.
    if (!(t instanceof HtmlTextView)) {
        t.setMovementMethod(LinkMovementMethod.getInstance());
    }
}