Java Code Examples for android.text.style.URLSpan

The following are top voted examples for showing how to use android.text.style.URLSpan. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Ghost-Android   File: AppUtils.java   Source Code and License 7 votes vote down vote up
public static void setHtmlWithLinkClickHandler(TextView tv, String html,
                                        Action1<String> linkClickHandler) {
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        int start = strBuilder.getSpanStart(span);
        int end = strBuilder.getSpanEnd(span);
        int flags = strBuilder.getSpanFlags(span);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                linkClickHandler.call(span.getURL());
            }
        };
        strBuilder.setSpan(clickable, start, end, flags);
        strBuilder.removeSpan(span);
    }
    tv.setText(strBuilder);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example 2
Project: GitHub   File: MyURLSpan.java   Source Code and License 6 votes vote down vote up
/**
 * @param context
 * @param spannable
 * @param title
 * @return
 */
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
	if (spannable == null || spannable.length() <= 0) {
		Log.e(TAG, "getText  spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
		return new SpannableStringBuilder();
	}

	SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
	builder.clearSpans();// should clear old spans
	URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
	if (urls != null) {
		for (URLSpan urlSpan : urls) {
			if (urlSpan != null) {
				MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
				builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
						.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}
		}
	}

	return builder;
}
 
Example 3
Project: GitHub   File: MyURLSpan.java   Source Code and License 6 votes vote down vote up
/**
 * @param context
 * @param spannable
 * @param title
 * @return
 */
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
	if (spannable == null || spannable.length() <= 0) {
		Log.e(TAG, "getText  spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
		return new SpannableStringBuilder();
	}

	SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
	builder.clearSpans();// should clear old spans
	URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
	if (urls != null) {
		for (URLSpan urlSpan : urls) {
			if (urlSpan != null) {
				MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
				builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
						.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}
		}
	}

	return builder;
}
 
Example 4
Project: Forge   File: GeneratorFragment.java   Source Code and License 6 votes vote down vote up
/**
 * Taken from Stack Overflow - https://stackoverflow.com/a/17201376/6052295
 * Adds links to a HTML string
 *
 * @param html        the HTML string to add links to
 * @param linkifyMask the link type
 * @return The spannable text with clickable links
 */

public static Spannable linkifyHtml(String html, int linkifyMask) {
    Spanned text = fromHtml(fromHtml(html).toString());
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}
 
Example 5
Project: yyox   File: CustomTextView.java   Source Code and License 6 votes vote down vote up
/**
 * 解析link
 *
 * @param tv_content
 * @param context
 */
private static void dealUnderLinesData(TextView tv_content, Context context) {
    CharSequence charSequence = tv_content.getText();
    if (charSequence instanceof Spannable) {
        tv_content.setText("");
        Spannable s = EmojiDisplay.spannableFilter(tv_content.getContext(),
                new SpannableStringBuilder(charSequence),
                charSequence,
                EmoticonsKeyboardUtils.getFontHeight(tv_content));
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            String clickText = s.subSequence(start, end).toString();
            URLSpanNoUnderline urlSpanNoUnderline = new URLSpanNoUnderline(context, span.getURL(), clickText);
            s.setSpan(urlSpanNoUnderline, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        tv_content.append(s);
    }
}
 
Example 6
Project: yyox   File: CustomTextView.java   Source Code and License 6 votes vote down vote up
/**
 * 解析机器人对话link
 *
 * @param textView
 */
private static void dealAILink(TextView textView, String type) {
    CharSequence charSequence = textView.getText();
    if (charSequence instanceof Spannable) {
        textView.setText("");
        Spannable s = (Spannable) charSequence;
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            String clickContent = s.subSequence(start, end).toString();
            AIURLSpan myURLSpan = new AIURLSpan(span.getURL(), type, clickContent, textView.getContext());
            s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.append(s);
    }
}
 
Example 7
Project: yyox   File: CustomTextView.java   Source Code and License 6 votes vote down vote up
/**
 * 解析富文本link
 *
 * @param textView
 */
private static void dealCustomLink(TextView textView) {
    CharSequence charSequence = textView.getText();
    if (charSequence instanceof Spannable) {
        textView.setText("");
        Spannable s = (Spannable) charSequence;
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            CustomClickSpan myURLSpan = new CustomClickSpan(textView.getContext(), span.getURL());
            s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.append(s);
    }
}
 
Example 8
Project: airgram   File: ChatMessageCell.java   Source Code and License 6 votes vote down vote up
@Override
protected void onLongPress() {
    if (pressedLink instanceof URLSpanNoUnderline) {
        URLSpanNoUnderline url = (URLSpanNoUnderline) pressedLink;
        if (url.getURL().startsWith("/")) {
            delegate.didPressedUrl(currentMessageObject, pressedLink, true);
            return;
        }
    } else if (pressedLink instanceof URLSpan) {
        delegate.didPressedUrl(currentMessageObject, pressedLink, true);
        return;
    }
    resetPressedLink(-1);
    if (buttonPressed != 0 || pressedBotButton != -1) {
        buttonPressed = 0;
        pressedBotButton = -1;
        invalidate();
    }
    if (delegate != null) {
        delegate.didLongPressed(this);
    }
}
 
Example 9
Project: APIJSON-Android-RxJava   File: MyURLSpan.java   Source Code and License 6 votes vote down vote up
/**
 * @param context
 * @param spannable
 * @param title
 * @return
 */
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
	if (spannable == null || spannable.length() <= 0) {
		Log.e(TAG, "getText  spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
		return new SpannableStringBuilder();
	}

	SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
	builder.clearSpans();// should clear old spans
	URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
	if (urls != null) {
		for (URLSpan urlSpan : urls) {
			if (urlSpan != null) {
				MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
				builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
						.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}
		}
	}

	return builder;
}
 
Example 10
Project: Nird2   File: UiUtils.java   Source Code and License 6 votes vote down vote up
public static void makeLinksClickable(TextView v) {
	SpannableStringBuilder ssb = new SpannableStringBuilder(v.getText());
	URLSpan[] spans = ssb.getSpans(0, ssb.length(), URLSpan.class);
	for (URLSpan span : spans) {
		int start = ssb.getSpanStart(span);
		int end = ssb.getSpanEnd(span);
		final String url = span.getURL();
		ssb.removeSpan(span);
		ClickableSpan cSpan = new ClickableSpan() {
			@Override
			public void onClick(View v2) {
				LinkDialogFragment f = LinkDialogFragment.newInstance(url);
				FragmentManager fm = ((AppCompatActivity) v2.getContext())
						.getSupportFragmentManager();
				f.show(fm, f.getUniqueTag());
			}
		};
		ssb.setSpan(cSpan, start, end, 0);
	}
	v.setText(ssb);
	v.setMovementMethod(ArticleMovementMethod.getInstance());
}
 
Example 11
Project: Nird2   File: UiUtils.java   Source Code and License 6 votes vote down vote up
public static void makeLinksClickable(TextView v) {
	SpannableStringBuilder ssb = new SpannableStringBuilder(v.getText());
	URLSpan[] spans = ssb.getSpans(0, ssb.length(), URLSpan.class);
	for (URLSpan span : spans) {
		int start = ssb.getSpanStart(span);
		int end = ssb.getSpanEnd(span);
		final String url = span.getURL();
		ssb.removeSpan(span);
		ClickableSpan cSpan = new ClickableSpan() {
			@Override
			public void onClick(View v2) {
				LinkDialogFragment f = LinkDialogFragment.newInstance(url);
				FragmentManager fm = ((AppCompatActivity) v2.getContext())
						.getSupportFragmentManager();
				f.show(fm, f.getUniqueTag());
			}
		};
		ssb.setSpan(cSpan, start, end, 0);
	}
	v.setText(ssb);
	v.setMovementMethod(ArticleMovementMethod.getInstance());
}
 
Example 12
Project: orgzly-android   File: OrgFormatterTest.java   Source Code and License 6 votes vote down vote up
public OrgSpannable(String str) {
    SpannableStringBuilder ssb = OrgFormatter.INSTANCE.parse(context, str);

    string = ssb.toString();

    Object[] allSpans = ssb.getSpans(0, ssb.length() - 1, Object.class);

    spans = new OrgSpan[allSpans.length];

    for (int i = 0; i < allSpans.length; i++) {
        spans[i] = new OrgSpan();

        spans[i].start = ssb.getSpanStart(allSpans[i]);
        spans[i].end = ssb.getSpanEnd(allSpans[i]);
        spans[i].className = allSpans[i].getClass().getSimpleName();

        if (allSpans[i] instanceof URLSpan) {
            spans[i].url = ((URLSpan)allSpans[i]).getURL();
        }
    }
}
 
Example 13
Project: PlusGram   File: ChatMessageCell.java   Source Code and License 6 votes vote down vote up
@Override
protected void onLongPress() {
    if (pressedLink instanceof URLSpanNoUnderline) {
        URLSpanNoUnderline url = (URLSpanNoUnderline) pressedLink;
        if (url.getURL().startsWith("/")) {
            delegate.didPressedUrl(currentMessageObject, pressedLink, true);
            return;
        }
    } else if (pressedLink instanceof URLSpan) {
        delegate.didPressedUrl(currentMessageObject, pressedLink, true);
        return;
    }
    resetPressedLink(-1);
    if (buttonPressed != 0 || pressedBotButton != -1) {
        buttonPressed = 0;
        pressedBotButton = -1;
        invalidate();
    }
    if (delegate != null) {
        delegate.didLongPressed(this);
    }
}
 
Example 14
Project: underlx   File: HelpFragment.java   Source Code and License 6 votes vote down vote up
private void setHtmlFromHelpFile(String file) {
    htmltv.setHtml(getHelpFileContents(file));
    htmltv.setText(RichTextUtils.replaceAll((Spanned) htmltv.getText(), URLSpan.class, new RichTextUtils.URLSpanConverter(), new RichTextUtils.ClickSpan.OnClickListener() {
        @Override
        public void onClick(String url) {
            if(mListener == null) {
                return;
            }
            if (url.startsWith("help:")) {
                mListener.onHelpLinkClicked(url.substring(5));
            } else if (url.startsWith("station:")) {
                mListener.onStationLinkClicked(url.substring(8));
            } else if (url.startsWith("mailto:")) {
                mListener.onMailtoLinkClicked(url.substring(7));
            } else {
                mListener.onLinkClicked(url);
            }
        }
    }));
}
 
Example 15
Project: Tusky   File: StatusDetailedViewHolder.java   Source Code and License 6 votes vote down vote up
private void setApplication(@Nullable Status.Application app) {
    if (app != null) {

        timestampInfo.append("  •  ");

        if (app.website != null) {
            URLSpan span = new CustomURLSpan(app.website);

            SpannableStringBuilder text = new SpannableStringBuilder(app.name);
            text.setSpan(span, 0, app.name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            timestampInfo.append(text);
            timestampInfo.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            timestampInfo.append(app.name);
        }
    }
}
 
Example 16
Project: Tusky   File: ComposeActivity.java   Source Code and License 6 votes vote down vote up
private void onUploadSuccess(final QueuedMedia item, Attachment media) {
    item.id = media.id;
    item.preview.setProgress(-1);
    item.readyStage = QueuedMedia.ReadyStage.UPLOADED;

    /* Add the upload URL to the text field. Also, keep a reference to the span so if the user
     * chooses to remove the media, the URL is also automatically removed. */
    item.uploadUrl = new URLSpan(media.textUrl);
    int end = 1 + media.textUrl.length();
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(' ');
    builder.append(media.textUrl);
    builder.setSpan(item.uploadUrl, 1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    int cursorStart = textEditor.getSelectionStart();
    int cursorEnd = textEditor.getSelectionEnd();
    textEditor.append(builder);
    textEditor.setSelection(cursorStart, cursorEnd);

    waitForMediaLatch.countDown();
}
 
Example 17
Project: MyTravelingDiary   File: InviteFriendsActivity.java   Source Code and License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_invite_friends);
    ButterKnife.bind(this);
    Intent intent = getIntent();
    String addresseeName = intent.getStringExtra("contactName");
    addresseePhone = intent.getStringExtra("contactPhone");
    mAddresseeName.setText(addresseeName);
    SpannableString  inviteMessage = new SpannableString(this.getResources().getString(R.string.mine_send_message));
    message = this.getResources().getString(R.string.mine_send_message);
    inviteMessage.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 54, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    inviteMessage.setSpan(new URLSpan("http://www.xingka.cc"), 54, 74,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    inviteMessage.setSpan(new ForegroundColorSpan(Color.BLACK), 74, 88, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    mInviteMessage.setText(inviteMessage);
    mInviteMessage.setMovementMethod(LinkMovementMethod.getInstance());

}
 
Example 18
Project: insapp-android   File: Utils.java   Source Code and License 6 votes vote down vote up
public static void convertToLinkSpan(Context context, TextView textView) {
    Spannable s = new SpannableString(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 LinkSpan(context, span.getURL());
        s.setSpan(span, start, end, 0);
    }

    textView.setText(s);
}
 
Example 19
Project: Paper-Tales   File: WallActivity.java   Source Code and License 6 votes vote down vote up
@Override
public CharSequence getTransformation(CharSequence src, View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        Linkify.addLinks(textView, Linkify.WEB_URLS);

        if (textView.getText() != null && textView.getText() instanceof Spannable) {
            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), end = text.getSpanEnd(oldSpan);
                String url = oldSpan.getURL();

                text.removeSpan(oldSpan);
                text.setSpan(new CustomTabsURLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return text;
        }
    }
    return src;
}
 
Example 20
Project: APReader   File: CustomTabsMovementMethod.java   Source Code and License 6 votes vote down vote up
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    x -= textView.getTotalPaddingLeft();
    y -= textView.getTotalPaddingTop();
    x += textView.getScrollX();
    y += textView.getScrollY();

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

    URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
    if (link.length > 0) {
        String linkText = link[0].getURL();
        if (Patterns.WEB_URL.matcher(linkText).matches())
            CustomTabsBuilder.open(context, Uri.parse(linkText));
        return true;
    }

    return false;
}
 
Example 21
Project: android-common-util-light   File: StyledText.java   Source Code and License 6 votes vote down vote up
/**
 * 设置url及点击这个url的监听器: text可以是url,电话,邮箱,地图,短信,彩信 等
 * <p>需要TextView.setMovementMethod(LinkMovementMethod.getInstance());响应(一般最后调用) </p>
 * <p>电话eg: msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  </p>
    <p>邮件eg: msp.setSpan(new URLSpan("mailto:[email protected]"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</p>
    <p>网址eg:msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     </p>
    <p>短信:msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //使用sms:或者smsto</p>
   <p>彩信: msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //使用mms:或者mmsto:</p>
   <p>地图: msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //地图   </p>
 @param text the context
 @param listener the click listener
    @return this
 */
public StyledText appendUrl(final CharSequence text,
        final OnClickListener listener) {
    return append(text, new URLSpan(text.toString()) {
        @Override
        public void onClick(View widget) {
            listener.onClick(widget);
            /** 默认用浏览器打开指定的Url
             *  Uri uri = Uri.parse(getURL());
    Context context = widget.getContext();
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
    context.startActivity(intent);
             */
        }
    });
}
 
Example 22
Project: Mediator_Android   File: SafeURLSpan.java   Source Code and License 6 votes vote down vote up
public static CharSequence replaceURLSpans(CharSequence text) {
    if (text instanceof Spannable) {
        final Spannable s = (Spannable) text;
        final URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        if (spans != null && spans.length > 0) {
            for (int i = spans.length - 1; i >= 0; i--) {
                final URLSpan span = spans[i];
                final int start = s.getSpanStart(span);
                final int end = s.getSpanEnd(span);
                final int flags = s.getSpanFlags(span);
                s.removeSpan(span);
                s.setSpan(new SafeURLSpan(span.getURL()), start, end, flags);
            }
        }
    }
    return text;
}
 
Example 23
Project: APIJSON   File: MyURLSpan.java   Source Code and License 6 votes vote down vote up
/**
 * @param context
 * @param spannable
 * @param title
 * @return
 */
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
	if (spannable == null || spannable.length() <= 0) {
		Log.e(TAG, "getText  spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
		return new SpannableStringBuilder();
	}

	SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
	builder.clearSpans();// should clear old spans
	URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
	if (urls != null) {
		for (URLSpan urlSpan : urls) {
			if (urlSpan != null) {
				MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
				builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
						.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}
		}
	}

	return builder;
}
 
Example 24
Project: Derpibooru   File: HtmlTextView.java   Source Code and License 6 votes vote down vote up
private void makeLinkClickable(SpannableStringBuilder strBuilder, URLSpan span) {
    final String url = span.getURL();
    /* http://stackoverflow.com/a/19989677/1726690 */
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            if ((!onLinkClicked(url)) && (mLinkListener != null)) {
                mLinkListener.onLinkClick(url);
            }
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 25
Project: xowa_android   File: WidgetProviderFeaturedPage.java   Source Code and License 6 votes vote down vote up
private String findFeaturedArticleTitle(String pageLeadContent) {
    // Extract the actual link to the featured page in a hacky way (until we
    // have the correct API for it):
    // Parse the HTML, and look for the first link, which should be the
    // article of the day.
    Spanned text = Html.fromHtml(pageLeadContent);
    URLSpan[] spans = text.getSpans(0, text.length(), URLSpan.class);
    String titleText = "";
    for (URLSpan span : spans) {
        if (!span.getURL().startsWith("/wiki/")
                || (text.getSpanEnd(span) - text.getSpanStart(span) <= 1)) {
            continue;
        }
        PageTitle title = WikipediaApp.getInstance().getPrimarySite()
                .titleForInternalLink(decodeURL(span.getURL()));
        if (!title.isFilePage() && !title.isSpecial()) {
            titleText = title.getDisplayText();
            break;
        }
    }
    return titleText;
}
 
Example 26
Project: xowa_android   File: LinkMovementMethodExt.java   Source Code and License 6 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull final TextView widget, @NonNull final Spannable buffer, @NonNull final MotionEvent event) {
    final int action = event.getAction();
    if (action == MotionEvent.ACTION_UP) {
        final int x = (int) event.getX() - widget.getTotalPaddingLeft() + widget.getScrollX();
        final int y = (int) event.getY() - widget.getTotalPaddingTop() + widget.getScrollY();
        final Layout layout = widget.getLayout();
        final int line = layout.getLineForVertical(y);
        final int off = layout.getOffsetForHorizontal(line, x);
        final URLSpan[] links = buffer.getSpans(off, off, URLSpan.class);
        if (links.length != 0) {
            String url = decodeURL(links[0].getURL());
            handler.onUrlClick(url);
            return true;
        }
    }
    return super.onTouchEvent(widget, buffer, event);
}
 
Example 27
Project: twicalico   File: TwitterStringUtils.java   Source Code and License 6 votes vote down vote up
public static Spanned convertUrlSpanToCustomTabs(Spanned spanned, Context context){
    SpannableStringBuilder builder = new SpannableStringBuilder(spanned);
    URLSpan[] spans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (URLSpan span : spans) {
        builder.removeSpan(span);
        builder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                new CustomTabsIntent.Builder()
                        .setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary))
                        .setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
                        .addDefaultShareMenuItem()
                        .build()
                        .launchUrl(context, Uri.parse(span.getURL()));
            }
        }, spanned.getSpanStart(span), spanned.getSpanEnd(span), spanned.getSpanFlags(span));
    }
    return builder;
}
 
Example 28
Project: e621-android   File: MiscStatics.java   Source Code and License 6 votes vote down vote up
public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
            /*if (span.getURL().startsWith("baseURL")) {
                //TODO stay in app
            } else  */
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(span.getURL()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //running from 'outside' and activity
            view.getContext().getApplicationContext().startActivity(intent);
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}
 
Example 29
Project: materialup   File: HtmlUtils.java   Source Code and License 6 votes vote down vote up
/**
 * Parse the given input using {@link TouchableUrlSpan}s
 * rather than vanilla {@link URLSpan}s so that they respond to touch.
 *
 * @param input
 * @param linkTextColor
 * @param linkHighlightColor
 * @return
 */
public static Spanned parseHtml(String input,
                                ColorStateList linkTextColor,
                                @ColorInt int linkHighlightColor) {
    SpannableStringBuilder spanned = (SpannableStringBuilder) Html.fromHtml(input);

    // strip any trailing newlines
    while (spanned.charAt(spanned.length() - 1) == '\n') {
        spanned = spanned.delete(spanned.length() - 1, spanned.length());
    }

    URLSpan[] urlSpans = spanned.getSpans(0, spanned.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
        int start = spanned.getSpanStart(urlSpan);
        int end = spanned.getSpanEnd(urlSpan);
        spanned.removeSpan(urlSpan);
        // spanned.subSequence(start, start + 1) == "@" TODO send to our own user activity...
        // when i've written it
        spanned.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor,
                linkHighlightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return spanned;
}
 
Example 30
Project: H-Viewer   File: HtmlContentParser.java   Source Code and License 6 votes vote down vote up
private static void setLinkClickable(Context context, final SpannableStringBuilder clickableHtmlBuilder,
                                     final URLSpan urlSpan, String sourceUrl) {
    int start = clickableHtmlBuilder.getSpanStart(urlSpan);
    int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
    int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
    ClickableSpan clickableSpan = new ClickableSpan() {
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            String url = urlSpan.getURL();
            url = TextUtils.isEmpty(url) ? "" : RegexValidateUtil.getAbsoluteUrlFromRelative(url, sourceUrl);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        }
    };
    clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
}
 
Example 31
Project: memoir   File: ConverterSpannedToHtml.java   Source Code and License 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example 32
Project: AyoSunny   File: HtmlHandler.java   Source Code and License 6 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 33
Project: BigApp_Discuz_Android   File: AdapterActManage.java   Source Code and License 6 votes vote down vote up
private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
                              final URLSpan urlSpan) {
    int start = clickableHtmlBuilder.getSpanStart(urlSpan);
    int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
    int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
    clickableHtmlBuilder.setSpan(new ClickableSpan() {
        public void onClick(View view) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }, start, end, flags);
}
 
Example 34
Project: S1-Next   File: PostViewModel.java   Source Code and License 6 votes vote down vote up
private CharSequence getPostFloor() {
    Post p = post.get();
    if (p == null) {
        return null;
    }
    String text = "#" + p.getCount();
    Spannable spannable = new SpannableString(text);
    URLSpan urlSpan = new URLSpan(StringUtils.EMPTY) {
        @Override
        public void onClick(@NonNull View widget) {
            showFloorActionMenu(widget);
        }
    };
    spannable.setSpan(urlSpan, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannable;
}
 
Example 35
Project: AyoSunny   File: HtmlTagHandler.java   Source Code and License 6 votes vote down vote up
private static void endA(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;

        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 36
Project: Amumu   File: HtmlUtils.java   Source Code and License 6 votes vote down vote up
private static SpannableStringBuilder linkifyPlainLinks(CharSequence input,
                                               ColorStateList linkTextColor,
                                       @ColorInt int linkHighlightColor) {
    final SpannableString plainLinks = new SpannableString(input); // copy of input
    Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

    final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);

    // add any plain links to the output
    final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
    for (URLSpan urlSpan : urlSpans) {
        ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
                plainLinks.getSpanStart(urlSpan),
                plainLinks.getSpanEnd(urlSpan),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return ssb;
}
 
Example 37
Project: DoneForAndroid   File: TaskListAdapter.java   Source Code and License 6 votes vote down vote up
private Spannable formatForTextView(Spannable p_Text) {
    URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
    Pattern hashTagURLPattern = Pattern.compile(".*\\/\\#tags\\/.*", Pattern.CASE_INSENSITIVE);
    Matcher hashtagMatcher = null;
    for (URLSpan span : spans) {
        hashtagMatcher = hashTagURLPattern.matcher(span.getURL());
        // Only format if it's a hashtag link
        if (hashtagMatcher.find()) {
            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);
        }
    }
    return p_Text;
}
 
Example 38
Project: DoneForAndroid   File: TaskDetailsFragment.java   Source Code and License 6 votes vote down vote up
private Spannable formatForTextView(Spannable p_Text) {
    URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
    Pattern hashTagURLPattern = Pattern.compile(".*\\/\\#tags\\/.*", Pattern.CASE_INSENSITIVE);
    Matcher hashtagMatcher = null;
    for (URLSpan span : spans) {
        hashtagMatcher = hashTagURLPattern.matcher(span.getURL());
        // Only format if it's a hashtag link
        if (hashtagMatcher.find()) {
            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);
        }
    }
    return p_Text;
}
 
Example 39
Project: RichEditText   File: HtmlToSpannedConverter.java   Source Code and License 6 votes vote down vote up
private static void endA(SpannableStringBuilder text)
{
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Href h = (Href) obj;
        if (h == null) {
            Log.e("endA", "h = null");
            return;
        }
        if (h.mHref != null) {
            text.setSpan(new URLSpan(h.mHref), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 40
Project: HashGirl   File: HashGirl.java   Source Code and License 6 votes vote down vote up
private SpannableString wrapIntoHashGirlURLSpan(String htmlText, final OnClickHashListener onClickHashListener) {
    SpannableString s = SpannableString.valueOf(Html.fromHtml(htmlText));
    URLSpan[] urlSpanList = s.getSpans(0, s.length(), URLSpan.class);
    for(URLSpan urlSpan : urlSpanList) {
        int start = s.getSpanStart(urlSpan);
        int end = s.getSpanEnd(urlSpan);
        s.removeSpan(urlSpan);
        HashGirlURLSpan hashGirlURLSpan = new HashGirlURLSpan(urlSpan.getURL(), new OnClickHashListener() {
            @Override
            public void onClickHash(String url) {
                if(onClickHashListener != null) {
                    onClickHashListener.onClickHash(url);
                }
            }
        });
        hashGirlURLSpan.setUnderline(underline);
        hashGirlURLSpan.setStrike(strike);
        hashGirlURLSpan.setColor(color);
        hashGirlURLSpan.setBgcolor(bgcolor);
        hashGirlURLSpan.setAlpha(alpha);
        s.setSpan(hashGirlURLSpan, start, end, 0);
    }
    return s;
}
 
Example 41
Project: stately   File: SparkleHelper.java   Source Code and License 6 votes vote down vote up
/**
 * Stylify text view to primary colour and no underline
 * @param c App context
 * @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());
        t.setLongClickable(false);
    }
}
 
Example 42
Project: jianguo   File: UrlUtils.java   Source Code and License 6 votes vote down vote up
/**
 * 让TextView自动解析URL并高亮设置点击链接(链接不支持中文)
 * Note:深深的体会到,写一个正则不容易啊,Android居然还不支持POSIX字符
 * <p/>
 * Created by kymjs(www.kymjs.com) on 8/5/15.
 *
 * @param tv      TextView
 * @param content 要高亮的内容
 * @return 已经解析之后的TextView
 */
public static TextView handleText(TextView tv, String content) {
    SpannableStringBuilder sp = new SpannableStringBuilder(content);
    //又碰上一个坑,在Android中的\p{Alnum}和Java中的\p{Alnum}不是同一个值,非得要我换成[a-zA-Z0-9]才行
    Pattern pattern = Pattern.compile("(http|https|ftp|svn)://([a-zA-Z0-9]+[/?.?])" +
            "+[a-zA-Z0-9]*\\??([a-zA-Z0-9]*=[a-zA-Z0-9]*&?)*");
    Matcher matcher = pattern.matcher(content);

    while (matcher.find()) {
        String url = matcher.group();
        int start = content.indexOf(url);
        if (start >= 0) {
            int end = start + url.length();
            sp.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sp.setSpan(getClickableSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    tv.setText(sp);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    return tv;
}
 
Example 43
Project: Doctor   File: ConverterSpannedToHtml.java   Source Code and License 6 votes vote down vote up
private void handleEndTag(CharacterStyle style) {
    if (style instanceof URLSpan) {
        mOut.append("</a>");
    } else if (style instanceof TypefaceSpan) {
        mOut.append("</font>");
    } else if (style instanceof ForegroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof BackgroundColorSpan) {
        mOut.append("</font>");
    } else if (style instanceof AbsoluteSizeSpan) {
        mOut.append("</font>");
    } else if (style instanceof StrikethroughSpan) {
        mOut.append("</strike>");
    } else if (style instanceof SubscriptSpan) {
        mOut.append("</sub>");
    } else if (style instanceof SuperscriptSpan) {
        mOut.append("</sup>");
    } else if (style instanceof UnderlineSpan) {
        mOut.append("</u>");
    } else if (style instanceof BoldSpan) {
        mOut.append("</b>");
    } else if (style instanceof ItalicSpan) {
        mOut.append("</i>");
    }
}
 
Example 44
Project: android-proguards   File: HtmlUtils.java   Source Code and License 6 votes vote down vote up
private static SpannableStringBuilder linkifyPlainLinks(
        CharSequence input,
        ColorStateList linkTextColor,
        @ColorInt int linkHighlightColor) {
    final SpannableString plainLinks = new SpannableString(input); // copy of input

    // Linkify doesn't seem to work as expected on M+
    // TODO: figure out why
    //Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

    final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);

    // add any plain links to the output
    final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
    for (URLSpan urlSpan : urlSpans) {
        ssb.removeSpan(urlSpan);
        ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
                plainLinks.getSpanStart(urlSpan),
                plainLinks.getSpanEnd(urlSpan),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return ssb;
}
 
Example 45
Project: android_chat_UI   File: UrlUtils.java   Source Code and License 6 votes vote down vote up
/**
 * 让TextView自动解析URL并高亮设置点击链接(链接不支持中文)
 * Note:深深的体会到,写一个正则不容易啊,Android居然还不支持POSIX字符
 * <p/>
 * Created by kymjs(www.kymjs.com) on 8/5/15.
 *
 * @param tv      TextView
 * @param content 要高亮的内容
 * @return 已经解析之后的TextView
 */
public static TextView handleText(TextView tv, String content) {
    SpannableStringBuilder sp = new SpannableStringBuilder(content);
    //又碰上一个坑,在Android中的\p{Alnum}和Java中的\p{Alnum}不是同一个值,非得要我换成[a-zA-Z0-9]才行
    Pattern pattern = Pattern.compile("(http|https|ftp|svn)://([a-zA-Z0-9]+[/?.?])" +
            "+[a-zA-Z0-9]*\\??([a-zA-Z0-9]*=[a-zA-Z0-9]*&?)*");
    Matcher matcher = pattern.matcher(content);

    while (matcher.find()) {
        String url = matcher.group();
        int start = content.indexOf(url);
        if (start >= 0) {
            int end = start + url.length();
            sp.setSpan(new URLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            sp.setSpan(getClickableSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    tv.setText(sp);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    return tv;
}
 
Example 46
Project: OldDriver-master   File: HtmlUtils.java   Source Code and License 6 votes vote down vote up
private static SpannableStringBuilder linkifyPlainLinks(CharSequence input,
                                               ColorStateList linkTextColor,
                                       @ColorInt int linkHighlightColor) {
    final SpannableString plainLinks = new SpannableString(input); // copy of input
    Linkify.addLinks(plainLinks, Linkify.WEB_URLS);

    final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);

    // add any plain links to the output
    final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
    for (URLSpan urlSpan : urlSpans) {
        ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
                plainLinks.getSpanStart(urlSpan),
                plainLinks.getSpanEnd(urlSpan),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return ssb;
}
 
Example 47
Project: Espresso   File: CompanyDetailFragment.java   Source Code and License 5 votes vote down vote up
@Override
public void setCompanyTel(String tel) {
    this.tel = tel;
    String companyTel = getString(R.string.phone_number) + "\n" + tel;
    Spannable spannable = new SpannableStringBuilder(companyTel);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, companyTel.length() - tel.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new URLSpan(tel), companyTel.length() - tel.length(), companyTel.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textViewTel.setText(spannable);
}
 
Example 48
Project: Espresso   File: CompanyDetailFragment.java   Source Code and License 5 votes vote down vote up
@Override
public void setCompanyWebsite(String website) {
    this.website = website;
    String ws = getString(R.string.official_website) + "\n" + website;
    Spannable spannable = new SpannableStringBuilder(ws);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, ws.length() - website.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new URLSpan(website), ws.length() - website.length(), ws.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textViewWebsite.setText(spannable);
}
 
Example 49
Project: Aequorea   File: CachedSpannedParser.java   Source Code and 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(), accentColor);
    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 50
Project: SpanEZ   File: SpanEZTest.java   Source Code and License 5 votes vote down vote up
@Test
public void link_should_add_only_one_span() {
    spanBuilder.link(range, "")
               .apply();

    verify((SpanEZ) spanBuilder, times(1))
            .addSpan(isA(TargetRange.class), isA(URLSpan.class));
}
 
Example 51
Project: Mix   File: AutoActivity.java   Source Code and License 5 votes vote down vote up
@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ywjj:
                Integer[] ints = {1, 4, 8, 2, 3, 8, 6};
                Arrays.sort(ints, Integer::compareTo);
                StringBuilder print = new StringBuilder("");
                for (int i = 0; i < ints.length; i++) {
                    print.append(ints[i]);
                }
                UIUtils.toastShort(AutoActivity.this, print.toString());
                break;
            case R.id.btn_new_advice:
                SpannableStringBuilder ssb = new SpannableStringBuilder(tvSickbedAddress.getText());
                ssb.setSpan(new URLSpan("https://www.baidu.com"), 0,
                        tvSickbedAddress.getText().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// 在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象,使用这种方式的弊端是,会让设置的maxLine失效
//                tvSickbedAddress.setText(ssb);
//                tvSickbedAddress.setMovementMethod(LinkMovementMethod.getInstance());
                tvSickbedAddress.setOnTouchListener(new TouchTextView(ssb));
// 设置点击后的颜色,这里涉及到ClickableSpan的点击背景
                tvSickbedAddress.setHighlightColor(0xff8FABCC);

                break;
            case R.id.btn_new_bed:
                break;
            default:
                break;
        }
    }
 
Example 52
Project: android_nextgis_mobile   File: ControlHelper.java   Source Code and License 5 votes vote down vote up
public static void highlightText(TextView textView)
{
    final CharSequence text = textView.getText();
    final SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(
            new URLSpan(""), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}
 
Example 53
Project: mvvm-template   File: BetterLinkMovementExtended.java   Source Code and License 5 votes vote down vote up
static BetterLinkMovementExtended.ClickableSpanWithText ofSpan(TextView textView, ClickableSpan span) {
    Spanned s = (Spanned) textView.getText();
    String text;
    if (span instanceof URLSpan) {
        text = ((URLSpan) span).getURL();
    } else {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        text = s.subSequence(start, end).toString();
    }
    return new BetterLinkMovementExtended.ClickableSpanWithText(span, text);
}
 
Example 54
Project: mvvm-template   File: SpannableBuilder.java   Source Code and License 5 votes vote down vote up
public SpannableBuilder url(final CharSequence text, final View.OnClickListener listener) {
    if (!InputHelper.isEmpty(text))
        return append(text, new URLSpan(text.toString()) {
            @Override
            public void onClick(View widget) {
                listener.onClick(widget);
            }
        });
    return this;
}
 
Example 55
Project: SDHtmlTextView   File: LinkHandler.java   Source Code and License 5 votes vote down vote up
@Override
public void handleTagNode(TagNode node, SpannableStringBuilder builder,
		int start, int end, SpanStack spanStack) {

	final String href = node.getAttributeByName("href");
	spanStack.pushSpan(new URLSpan(href), start, end);
}
 
Example 56
Project: robird-reborn   File: LinkUtils.java   Source Code and License 5 votes vote down vote up
private static void addCustomTabUrlHandler(Activity activity, TextView textView) {
    CharSequence text = textView.getText();
    Spannable s = new SpannableString(text);
    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 CustomTabUrlSpan(activity, span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
 
Example 57
Project: PlusGram   File: DialogsActivity.java   Source Code and License 5 votes vote down vote up
public void didPressedUrl(final ClickableSpan url) {
    if (url == null) {
        return;
    }
    if (url instanceof URLSpanUserMention) {
        TLRPC.User user = MessagesController.getInstance().getUser(Utilities.parseInt(((URLSpanUserMention) url).getURL()));
        if (user != null) {
            MessagesController.openChatOrProfileWith(user, null, DialogsActivity.this, 0, false);
        }
    } else if (url instanceof URLSpanNoUnderline) {
        String str = ((URLSpanNoUnderline) url).getURL();
        if (str.startsWith("@")) {
            MessagesController.openByUserName(str.substring(1), DialogsActivity.this, 0);
        }
    } else {
        final String urlFinal = ((URLSpan) url).getURL();

        if (((URLSpan) url).getURL().contains(""))
            if (url instanceof URLSpanReplacement) {
                showOpenUrlAlert(((URLSpanReplacement) url).getURL(), true);
            } else if (url instanceof URLSpan) {
                Browser.openUrl(getParentActivity(), urlFinal, true);
            } else {
                url.onClick(fragmentView);
            }

    }
}
 
Example 58
Project: CustomAndroidOneSheeld   File: URLSpanNoUnderline.java   Source Code and License 5 votes vote down vote up
public static void stripUnderlines(TextView textView) {
    Spannable s = new SpannableString(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 59
Project: Cable-Android   File: ConversationItem.java   Source Code and License 5 votes vote down vote up
private SpannableString linkifyMessageBody(SpannableString messageBody, boolean shouldLinkifyAllLinks) {
  boolean hasLinks = Linkify.addLinks(messageBody, shouldLinkifyAllLinks ? Linkify.ALL : 0);

  if (hasLinks) {
    URLSpan[] urlSpans = messageBody.getSpans(0, messageBody.length(), URLSpan.class);
    for (URLSpan urlSpan : urlSpans) {
      int start = messageBody.getSpanStart(urlSpan);
      int end = messageBody.getSpanEnd(urlSpan);
      messageBody.setSpan(new LongClickCopySpan(urlSpan.getURL()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
  }
  return messageBody;
}
 
Example 60
Project: react-native-udesk   File: MessageAdatper.java   Source Code and License 5 votes vote down vote up
@Override
void bind(Context context) {
    try {
        CharSequence charSequence = Html.fromHtml(message.getMsgContent());
        String msg = charSequence.toString();
        if (msg.endsWith("\n\n")) {
             charSequence = charSequence.subSequence(0, charSequence.length() - 2);
        }
        rich_tvmsg.setText(charSequence);
        rich_tvmsg.setMovementMethod(LinkMovementMethod.getInstance());
        CharSequence text = rich_tvmsg.getText();
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) rich_tvmsg.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.clearSpans();// should clear old spans
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                style.setSpan(myURLSpan, sp.getSpanStart(url),
                        sp.getSpanEnd(url),
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            }
            rich_tvmsg.setText(style);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError error) {
        error.printStackTrace();
    }

}