Java Code Examples for android.text.style.URLSpan#getURL()

The following examples show how to use android.text.style.URLSpan#getURL() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkTransformationMethod.java    From EdXposedManager with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        Linkify.addLinks(textView, Linkify.WEB_URLS);
        if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
            return source;
        }
        Spannable text = (Spannable) textView.getText();
        URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
        for (int i = spans.length - 1; i >= 0; i--) {
            URLSpan oldSpan = spans[i];
            int start = text.getSpanStart(oldSpan);
            int end = text.getSpanEnd(oldSpan);
            String url = oldSpan.getURL();
            text.removeSpan(oldSpan);
            text.setSpan(new CustomTabsURLSpan(activity, url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return text;
    }
    return source;
}
 
Example 2
Source File: IMChatBaseView.java    From imsdk-android with MIT License 6 votes vote down vote up
private void handleUrlSpan() {
    mTimeTextView.setMovementMethod(LinkMovementMethod.getInstance());
    CharSequence text = mTimeTextView.getText();
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
    if (text instanceof Spannable) {
        int end = text.length();
        Spannable spannable = (Spannable) mTimeTextView.getText();
        URLSpan[] urlSpans = spannable.getSpans(0, end, URLSpan.class);
        if (urlSpans.length == 0) {
            return;
        }
        // 循环遍历并拦截
        for (URLSpan uri : urlSpans) {
            String url = uri.getURL();
            if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
                CustomUrlSpan customUrlSpan = new CustomUrlSpan(context, url);
                spannableStringBuilder.setSpan(customUrlSpan, spannable.getSpanStart(uri),
                        spannable.getSpanEnd(uri), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            spannableStringBuilder.removeSpan(uri);
        }
        mTimeTextView.setText(spannableStringBuilder);
    }
}
 
Example 3
Source File: LinkTransformationMethod.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
            return source;
        }
        SpannableString text = (SpannableString) textView.getText();
        URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
        for (int i = spans.length - 1; i >= 0; i--) {
            URLSpan oldSpan = spans[i];
            int start = text.getSpanStart(oldSpan);
            int end = text.getSpanEnd(oldSpan);
            String url = oldSpan.getURL();
            text.removeSpan(oldSpan);
            text.setSpan(new CustomUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return text;
    }
    return source;
}
 
Example 4
Source File: PmDetailAdapter.java    From TLint with Apache License 2.0 6 votes vote down vote up
private void handlerSpan(URLSpan span) {
    String url = span.getURL();
    if (TextUtils.isEmpty(url)) {
        return;
    }
    Uri uri = Uri.parse(url);
    switch (uri.getScheme()) {
        case "kanqiu":
            if (url.contains("topic")) {
                String tid = uri.getLastPathSegment();
                ContentActivity.startActivity(mActivity, "", tid, "", 1);
            } else if (url.contains("board")) {
                String boardId = uri.getLastPathSegment();
                ThreadListActivity.startActivity(mActivity, boardId);
            }
            break;
        case "app":
            break;
        case "http":
        case "https":
            BrowserActivity.startActivity(mActivity, url);
            break;
    }
}
 
Example 5
Source File: LinksHelper.java    From X-Alarm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Remove underline
 * @param textView
 */
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);
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }

    textView.setText(s);
}
 
Example 6
Source File: CachedSpannedParser.java    From RichText with MIT License 5 votes vote down vote up
private void resetLinkSpan(SpannableStringBuilder ssb, RichTextConfig config, URLSpan urlSpan) {
    int start = ssb.getSpanStart(urlSpan);
    int end = ssb.getSpanEnd(urlSpan);
    ssb.removeSpan(urlSpan);
    LinkHolder linkHolder = new LinkHolder(urlSpan.getURL());
    if (config.linkFixCallback != null) {
        config.linkFixCallback.fix(linkHolder);
    }
    LongClickableURLSpan longClickableURLSpan = new LongClickableURLSpan(linkHolder, config.onUrlClickListener, config.onUrlLongClickListener);
    ssb.setSpan(longClickableURLSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
 
Example 7
Source File: HtmlTextView.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * Parses String containing HTML to Android's Spannable format and displays it in this TextView.
 *
 * @param html String containing HTML, for example: "<b>Hello world!</b>"
 */
public void setHtmlFromString(String html) {
    SpannableStringBuilder spanText = (SpannableStringBuilder) Html.fromHtml(html, new UrlImageGetter(this, getContext(), mImageLoader), new HtmlTagHandler());

    URLSpan[] spans = spanText.getSpans(0, spanText.length(), URLSpan.class);
    // TODO URLSpan 必需用自定义的 ClickableSpan (DiscourseURLSpan) 替换, 否则 URLSpan不可点击, 显示也没有下划线。 WHY?
    for (URLSpan span : spans) {
        int start = spanText.getSpanStart(span);
        int end = spanText.getSpanEnd(span);
        String url = span.getURL();
        if (url != null) {
            spanText.removeSpan(span);
            ClickableSpan span1 = new DiscourseURLSpan(span.getURL());
            spanText.setSpan(span1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    // ImageSpan[] images = spanText.getSpans(0, spanText.length(), ImageSpan.class);
    // for (ImageSpan image : images) {
    // int start = spanText.getSpanStart(image);
    // int end = spanText.getSpanEnd(image);
    // L.i("%d-%d :%s", start, end, image.getSource());
    // spanText.removeSpan(image);
    // DynamicDrawableSpan is = new DiscourseImageSpan(getContext(), image.getDrawable());
    // spanText.setSpan(is, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    //
    // }
    setText(spanText);

    // make links work
    setMovementMethod(LinkMovementMethod.getInstance());

    // no flickering when clicking textview for Android < 4
    // text.setTextColor(getResources().getColor(android.R.color.secondary_text_dark_nodisable));
}
 
Example 8
Source File: EmulatorView.java    From Ansole with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the URL for the link displayed at the specified screen coordinates.
 *
 * @param x The x coordinate being queried (from 0 to screen width)
 * @param y The y coordinate being queried (from 0 to screen height)
 * @return The URL for the link at the specified screen coordinates, or
 * null if no link exists there.
 */
public String getURLat(float x, float y) {
    float w = getWidth();
    float h = getHeight();

    //Check for division by zero
    //If width or height is zero, there are probably no links around, so return null.
    if (w == 0 || h == 0)
        return null;

    //Get fraction of total screen
    float x_pos = x / w;
    float y_pos = y / h;

    //Convert to integer row/column index
    int row = (int) Math.floor(y_pos * mRows);
    int col = (int) Math.floor(x_pos * mColumns);

    //Grab row from link layer
    URLSpan[] linkRow = mLinkLayer.get(row);
    URLSpan link;

    //If row exists, and link exists at column, return it
    if (linkRow != null && (link = linkRow[col]) != null)
        return link.getURL();
    else
        return null;
}
 
Example 9
Source File: RuleSpannables.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a menu item for URLSpan. <strong>Note: </strong> This method will not create menu item
 * for relative URLs.
 */
private @Nullable ContextMenuItem createMenuItemForUrlSpan(
    Context context,
    ContextMenuItemBuilder menuItemBuilder,
    int itemId,
    Spannable spannable,
    URLSpan span) {
  final String url = span.getURL();
  final int start = spannable.getSpanStart(span);
  final int end = spannable.getSpanEnd(span);
  if (start < 0 || end < 0) {
    return null;
  }
  final CharSequence label = spannable.subSequence(start, end);
  if (TextUtils.isEmpty(url) || TextUtils.isEmpty(label)) {
    return null;
  }

  final Uri uri = Uri.parse(url);
  if (uri.isRelative()) {
    // Generally, only absolute URIs are resolvable to an activity
    return null;
  }

  // Strip out ClickableSpans/UrlSpans from the label text.
  // A11y framework has changed how it handles double-tap from O. It's possible that double-tap
  // on the menu item will invoke ClickableSpans in the label text instead of calling
  // MenuItemClickListener. Thus we should remove ClickableSpans from label text.
  // Also apply this rule to pre-O in order to have consistent text appearance.
  SpannableUtils.stripTargetSpanFromText(label, TARGET_SPAN_CLASS);
  final ContextMenuItem item =
      menuItemBuilder.createMenuItem(context, R.id.group_links, itemId, Menu.NONE, label);
  item.setOnMenuItemClickListener(new UrlSpanMenuItemClickListener(context, uri));
  return item;
}
 
Example 10
Source File: QuestionWidget.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
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);
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
 
Example 11
Source File: U.java    From datmusic-android with Apache License 2.0 5 votes vote down vote up
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);
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
 
Example 12
Source File: PatternsHelper.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
public static void removeUnderlines(final TextView textView) {
    final Spannable s = (Spannable) textView.getText();
    final URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        final int start = s.getSpanStart(span);
        final int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new LinkifyCompat.URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
 
Example 13
Source File: RichTextView.java    From YiBo with Apache License 2.0 5 votes vote down vote up
private void stripUnderlines(Spannable s) {
    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(span.getURL());
        s.setSpan(span, start, end, 0);
    }
}
 
Example 14
Source File: TimeLineUtility.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
public static SpannableString convertNormalStringToSpannableString(String txt) {
    // hack to fix android imagespan bug,see
    // http://stackoverflow.com/questions/3253148/imagespan-is-cut-off-incorrectly-aligned
    // if string only contains emotion tags,add a empty char to the end
    String hackTxt;
    if (txt.startsWith("[") && txt.endsWith("]")) {
        hackTxt = txt + " ";
    } else {
        hackTxt = txt;
    }
    // SpannableString value = SpannableString.valueOf(hackTxt);

    String formatted = formatLink(hackTxt);

    Spanned spanned = Html.fromHtml(formatted);

    SpannableString value = new SpannableString(spanned);

    Linkify.addLinks(value, WeiboPatterns.MENTION_URL, WeiboPatterns.MENTION_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.WEB_URL, WeiboPatterns.WEB_SCHEME);
    Linkify.addLinks(value, WeiboPatterns.TOPIC_URL, WeiboPatterns.TOPIC_SCHEME);

    URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class);
    MyURLSpan weiboSpan;
    for (URLSpan urlSpan : urlSpans) {
        weiboSpan = new MyURLSpan(urlSpan.getURL());
        int start = value.getSpanStart(urlSpan);
        int end = value.getSpanEnd(urlSpan);
        value.removeSpan(urlSpan);
        value.setSpan(weiboSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    TimeLineUtility.addEmotions(value);
    return value;
}
 
Example 15
Source File: RichLinkViewTelegram.java    From RichLinkPreview with Apache License 2.0 5 votes vote down vote up
private static void removeUnderlines(Spannable p_Text) {
    URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);

    for(URLSpan span:spans) {
        int start = p_Text.getSpanStart(span);
        int end = p_Text.getSpanEnd(span);
        p_Text.removeSpan(span);
        span = new URLSpanNoUnderline(span.getURL());
        p_Text.setSpan(span, start, end, 0);
    }
}
 
Example 16
Source File: AccountEmailPasswordFragment.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
private void stripUnderlines(@NonNull TextView tv) {
   Spannable s = (Spannable) tv.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(span.getURL());
      s.setSpan(span, start, end, 0);
   }

   tv.setText(s);
}
 
Example 17
Source File: SpanParser.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
private HtmlLink getHtmlLink(URLSpan span, String content, int start, int end, float thisXOffset){
    HtmlLink  obj = new HtmlLink(content, start, end, thisXOffset, mFlowTextView.getLinkPaint(), span.getURL());
    mLinks.add(obj);
    return obj;
}
 
Example 18
Source File: SpanParser.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
private HtmlLink getHtmlLink(URLSpan span, String content, int start, int end, float thisXOffset){
    HtmlLink  obj = new HtmlLink(content, start, end, thisXOffset, mFlowTextView.getLinkPaint(), span.getURL());
    mLinks.add(obj);
    return obj;
}
 
Example 19
Source File: FlowTextView.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
private HtmlLink getHtmlLink(URLSpan span, String content, int start, int end, float thisXOffset) {
    HtmlLink obj = new HtmlLink(content, start, end, thisXOffset, mLinkPaint, span.getURL());
    mLinks.add(obj);
    return obj;
}
 
Example 20
Source File: MessageListItem.java    From zom-android-matrix with Apache License 2.0 4 votes vote down vote up
@Override
public NewTaskUrlSpan convert(URLSpan span) {
    return (new NewTaskUrlSpan(span.getURL()));
}