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: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]);/*from www .ja  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.pocketsoap.convodroid.AuthorMessageFragment.java

@Override
public void onClick(View v) {
    Log.i("Convodroid", "send " + messageText.getText() + " to " + recipients.getText());
    Spannable r = (Spannable) recipients.getText();
    sendButton.setEnabled(false);//from ww w.  ja  va2 s .c om
    UserSpan[] users = r.getSpans(0, r.length(), UserSpan.class);
    List<String> recipients = new ArrayList<String>(users.length);
    for (UserSpan us : users)
        recipients.add(us.user.id);
    NewMessage m = new NewMessage();
    m.recipients = recipients;
    m.body = messageText.getText().toString();
    final RestRequest req = ChatterRequests.postMessage(m);
    getLoaderManager().restartLoader(0, null, new LoaderCallbacks<String>() {

        @Override
        public Loader<String> onCreateLoader(int id, Bundle args) {
            return new JsonLoader<String>(getActivity(), client, req, new TypeReference<String>() {
            });
        }

        @Override
        public void onLoadFinished(Loader<String> loader, String val) {
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        }

        @Override
        public void onLoaderReset(Loader<String> loader) {
        }
    });
}

From source file:com.example.activity.ProfileActivity.java

private void setPlayer() {
    ImageLoader.getInstance().displayImage(playerModel.player.avatar_url, head, BeeFrameworkApp.options_head);
    name.setText(playerModel.player.name);
    location.setText(playerModel.player.location);

    shots_count.setText(playerModel.player.shots_count + "");
    likes_count.setText(playerModel.player.likes_count + "");
    following_count.setText(playerModel.player.following_count + "");
    followers_count.setText(playerModel.player.followers_count + "");

    net.setText(playerModel.player.website_url);

    CharSequence text = net.getText();
    if (text instanceof Spannable) {
        int end = text.length();
        Spannable sp = (Spannable) net.getText();
        URLSpan[] spans = sp.getSpans(0, end, URLSpan.class);
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.clearSpans();// should clear old spans
        for (URLSpan span : spans) {
            JayceSpan mySpan = new JayceSpan(span.getURL());
            style.setSpan(mySpan, sp.getSpanStart(span), sp.getSpanEnd(span),
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }/*w w  w. ja  v  a 2  s.c  o m*/
        net.setText(style);
    }
}

From source file:org.telegram.ui.Cells.BotHelpCell.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    boolean result = false;
    if (textLayout != null) {
        if (event.getAction() == MotionEvent.ACTION_DOWN
                || pressedLink != null && event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                resetPressedLink();//w  w  w. j  a v a2 s. c o m
                try {
                    int x2 = (int) (x - textX);
                    int y2 = (int) (y - textY);
                    final int line = textLayout.getLineForVertical(y2);
                    final int off = textLayout.getOffsetForHorizontal(line, x2);

                    final float left = textLayout.getLineLeft(line);
                    if (left <= x2 && left + textLayout.getLineWidth(line) >= x2) {
                        Spannable buffer = (Spannable) textLayout.getText();
                        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
                        if (link.length != 0) {
                            resetPressedLink();
                            pressedLink = link[0];
                            result = true;
                            try {
                                int start = buffer.getSpanStart(pressedLink);
                                urlPath.setCurrentLayout(textLayout, start, 0);
                                textLayout.getSelectionPath(start, buffer.getSpanEnd(pressedLink), urlPath);
                            } catch (Exception e) {
                                FileLog.e("tmessages", e);
                            }
                        } else {
                            resetPressedLink();
                        }
                    } else {
                        resetPressedLink();
                    }
                } catch (Exception e) {
                    resetPressedLink();
                    FileLog.e("tmessages", e);
                }
            } else if (pressedLink != null) {
                try {
                    if (pressedLink instanceof URLSpanNoUnderline) {
                        String url = ((URLSpanNoUnderline) pressedLink).getURL();
                        if (url.startsWith("@") || url.startsWith("#") || url.startsWith("/")) {
                            if (delegate != null) {
                                delegate.didPressUrl(url);
                            }
                        }
                    } else {
                        if (pressedLink instanceof URLSpan) {
                            Browser.openUrl(getContext(), ((URLSpan) pressedLink).getURL());
                        } else {
                            pressedLink.onClick(this);
                        }
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
                resetPressedLink();
                result = true;
            }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            resetPressedLink();
        }
    }
    return result || super.onTouchEvent(event);
}

From source file:org.telegram.ui.Cells.AboutLinkCell.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    if (Build.VERSION.SDK_INT >= 21 && getBackground() != null) {
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            getBackground().setHotspot(x, y);
        }/* www .  ja  v a 2  s . c o  m*/
    }

    boolean result = false;
    if (textLayout != null) {
        if (event.getAction() == MotionEvent.ACTION_DOWN
                || pressedLink != null && event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                resetPressedLink();
                try {
                    int x2 = (int) (x - textX);
                    int y2 = (int) (y - textY);
                    final int line = textLayout.getLineForVertical(y2);
                    final int off = textLayout.getOffsetForHorizontal(line, x2);

                    final float left = textLayout.getLineLeft(line);
                    if (left <= x2 && left + textLayout.getLineWidth(line) >= x2) {
                        Spannable buffer = (Spannable) textLayout.getText();
                        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
                        if (link.length != 0) {
                            resetPressedLink();
                            pressedLink = link[0];
                            result = true;
                            try {
                                int start = buffer.getSpanStart(pressedLink);
                                urlPath.setCurrentLayout(textLayout, start, 0);
                                textLayout.getSelectionPath(start, buffer.getSpanEnd(pressedLink), urlPath);
                            } catch (Exception e) {
                                FileLog.e("tmessages", e);
                            }
                        } else {
                            resetPressedLink();
                        }
                    } else {
                        resetPressedLink();
                    }
                } catch (Exception e) {
                    resetPressedLink();
                    FileLog.e("tmessages", e);
                }
            } else if (pressedLink != null) {
                try {
                    if (pressedLink instanceof URLSpanNoUnderline) {
                        String url = ((URLSpanNoUnderline) pressedLink).getURL();
                        if (url.startsWith("@") || url.startsWith("#") || url.startsWith("/")) {
                            if (delegate != null) {
                                delegate.didPressUrl(url);
                            }
                        }
                    } else {
                        if (pressedLink instanceof URLSpan) {
                            Browser.openUrl(getContext(), ((URLSpan) pressedLink).getURL());
                        } else {
                            pressedLink.onClick(this);
                        }
                    }
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
                resetPressedLink();
                result = true;
            }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            resetPressedLink();
        }
    }
    return result || super.onTouchEvent(event);
}

From source file:com.slx.funstream.adapters.ChatAdapter.java

private void linksToImages(Spannable spannable, TextView textView) {
    //      Matcher matcher = Linkify.WEB_URLS.matcher(spannable);
    //      URLSpan[] spans = textView.getUrls();
    URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    Log.d(TAG, "URLSpan[] size=" + spans.length);
    for (URLSpan span : spans) {
        String url = span.getURL();
        Log.d(TAG, "START: " + spannable.getSpanStart(span) + "END: " + spannable.getSpanEnd(span) + " " + url);
        ImageTarget t = new ImageTarget(spannable.getSpanStart(span), spannable.getSpanEnd(span), textView,
                spannable);/*from   w w w. j  a  va2  s .c o  m*/
        picasso.load(url).resize(300, 300).centerInside().onlyScaleDown().into(t);
        targets.add(t);
    }
}

From source file:com.silentcircle.accounts.AccountStep1.java

private 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 ww  w.  j  a va2s. co m*/
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:com.microsoft.azure.engagement.ProductDiscountActivity.java

/**
 * Method that adds or removes a strike from a TextView object
 *
 * @param textView The textView to manage
 *//*from  w ww  . j a  va2s  . co m*/
private final void addOrRemoveStrikeTextView(TextView textView, boolean toAdd) {
    textView.setText(textView.getText().toString(), TextView.BufferType.SPANNABLE);
    final Spannable spannable = (Spannable) textView.getText();

    if (toAdd == true) {
        // Add a StrikethroughSpan style
        final StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        spannable.setSpan(strikethroughSpan, 0, textView.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        // Remove only StrikethroughSpan style
        final Object spans[] = spannable.getSpans(0, textView.length(), Object.class);
        for (final Object span : spans) {
            if (span instanceof StrikethroughSpan == true) {
                spannable.removeSpan(span);
                return;
            }
        }
    }
}

From source file:com.amitupadhyay.aboutexample.ui.widget.FabOverlapTextView.java

private TouchableUrlSpan getPressedSpan(Spannable spannable, MotionEvent event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

    x -= getPaddingLeft();/*  w w  w  .j  a  v a  2 s.  com*/
    y -= getPaddingTop();

    x += getScrollX();
    y += getScrollY();

    int line = layout.getLineForVertical(y);
    int off = layout.getOffsetForHorizontal(line, x);

    TouchableUrlSpan[] link = spannable.getSpans(off, off, TouchableUrlSpan.class);
    TouchableUrlSpan touchedSpan = null;
    if (link.length > 0) {
        touchedSpan = link[0];
    }
    return touchedSpan;
}

From source file:com.waz.zclient.pages.main.conversation.views.row.message.views.TextMessageWithTimestamp.java

private boolean isClickInsideLink(MotionEvent event) {
    Object text = messageTextView.getText();
    if (text != null && text instanceof Spanned) {
        Spannable buffer = (Spannable) text;

        int x = (int) event.getX();
        int y = (int) event.getY();

        x -= messageTextView.getTotalPaddingLeft();
        y -= messageTextView.getTotalPaddingTop();

        x += messageTextView.getScrollX();
        y += messageTextView.getScrollY();

        Layout layout = messageTextView.getLayout();
        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        return link.length > 0;

    }//from ww w  . j  av  a2 s .c o  m
    return false;
}