Example usage for android.text Spannable getSpanEnd

List of usage examples for android.text Spannable getSpanEnd

Introduction

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

Prototype

public int getSpanEnd(Object tag);

Source Link

Document

Return the end of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

Usage

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 a 2  s  . c o  m
    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);/*w ww. j av a  2  s .c  om*/
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

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/*from   w w w.j a  va2 s . c om*/
            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: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  . ja  v a  2  s  .c om*/
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

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;/* w  w w .  j a va  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;// ww w.  j a v  a 2s  .  com
                    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 ww  w.  j a  v  a2s  .com
                    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
 * /*from   ww w  .ja  va 2 s .co 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  av  a2  s .  c om*/
 * @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/*from w  w  w. j  a v a2  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);
}