List of usage examples for android.text Spannable getSpanEnd
public int getSpanEnd(Object tag);
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 ww. j a v a 2 s .c om*/ return chooser.build(); } } return null; }
From source file:com.lloydtorres.stately.helpers.SparkleHelper.java
/** * Stylify text view to primary colour and no underline * @param c App context/*from w w w. j ava 2s . c o 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()); } }
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 . jav a2s . co 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.android.mail.browse.MessageHeaderView.java
private static void stripUnderlines(TextView textView, Account account) { final Spannable spannable = (Spannable) textView.getText(); final URLSpan[] urls = textView.getUrls(); for (URLSpan span : urls) { final int start = spannable.getSpanStart(span); final int end = spannable.getSpanEnd(span); spannable.removeSpan(span);/*from w w w .jav a 2s . c o m*/ span = new EmailAddressSpan(account, span.getURL().substring(7)); spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } }
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 ww.ja v a 2s . c o m net.setText(style); } }
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 w w w . ja va 2 s .c o m span = new URLSpanNoUnderline(span.getURL()); s.setSpan(span, start, end, 0); } textView.setText(s); }
From source file:com.amitupadhyay.aboutexample.ui.widget.FabOverlapTextView.java
/** * This is why you don't implement your own TextView kids; you have to handle everything! *//*w ww . j ava 2 s. c o m*/ @Override public boolean onTouchEvent(MotionEvent event) { if (!(text instanceof Spanned)) return super.onTouchEvent(event); Spannable spannedText = (Spannable) text; boolean handled = false; if (event.getAction() == MotionEvent.ACTION_DOWN) { pressedSpan = getPressedSpan(spannedText, event); if (pressedSpan != null) { pressedSpan.setPressed(true); Selection.setSelection(spannedText, spannedText.getSpanStart(pressedSpan), spannedText.getSpanEnd(pressedSpan)); handled = true; postInvalidateOnAnimation(); } } else if (event.getAction() == MotionEvent.ACTION_MOVE) { TouchableUrlSpan touchedSpan = getPressedSpan(spannedText, event); if (pressedSpan != null && touchedSpan != pressedSpan) { pressedSpan.setPressed(false); pressedSpan = null; Selection.removeSelection(spannedText); postInvalidateOnAnimation(); } } else if (event.getAction() == MotionEvent.ACTION_UP) { if (pressedSpan != null) { pressedSpan.setPressed(false); pressedSpan.onClick(this); handled = true; postInvalidateOnAnimation(); } pressedSpan = null; Selection.removeSelection(spannedText); } else { if (pressedSpan != null) { pressedSpan.setPressed(false); handled = true; postInvalidateOnAnimation(); } pressedSpan = null; Selection.removeSelection(spannedText); } return handled; }
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);// ww w . j a va 2 s . c om picasso.load(url).resize(300, 300).centerInside().onlyScaleDown().into(t); targets.add(t); } }
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();/*from w ww .j ava 2 s . co 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); }//from ww w . j a v a 2 s . com } 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); }