Example usage for android.text Spannable removeSpan

List of usage examples for android.text Spannable removeSpan

Introduction

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

Prototype

public void removeSpan(Object what);

Source Link

Document

Remove the specified object from the range of text to which it was attached, if any.

Usage

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  ww  .  ja v  a2 s  .  c o 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. j ava  2 s . 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/*w ww.  j ava2s.c o  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:org.catnut.util.CatnutUtils.java

/**
 * textview// www.j  a  v  a  2s.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: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);
    }/*from   www .  j  av  a2s . com*/
}

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/*  www  . j a  v  a 2  s  .co 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:org.miaowo.miaowo.util.Html.java

private static void setSpanFromMark(Spannable text, Object mark, Object... spans) {
    int where = text.getSpanStart(mark);
    text.removeSpan(mark);
    int len = text.length();
    if (where != len) {
        for (Object span : spans) {
            text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }/*w w w .j  a va 2s .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  v  a  2s . c  om
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static void highlightSpans(Spannable text, int colour) {
    // Strip all existing colour spans.
    int n = text.length();
    ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
    for (int i = oldSpans.length - 1; i >= 0; i--) {
        text.removeSpan(oldSpans[i]);
    }//w ww  . j a v  a  2s. co  m
    // Colour the mentions and hashtags.
    String string = text.toString();
    int start;
    int end = 0;
    while (end < n) {
        char[] chars = { '#', '@' };
        FindCharsResult found = findStart(string, end, chars);
        start = found.stringIndex;
        if (start < 0) {
            break;
        }
        if (found.charIndex == 0) {
            end = findEndOfHashtag(string, start);
        } else if (found.charIndex == 1) {
            end = findEndOfMention(string, start);
        } else {
            break;
        }
        if (end < 0) {
            break;
        }
        text.setSpan(new ForegroundColorSpan(colour), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

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

/**
 * Stylify text view to primary colour and no underline
 * @param c App context// ww w.jav a2s . 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());
    }
}