Android - Enabling Only URL Link, But Disabling The Others When It Touches Something Non-URL Link In TextView

- 1 answer

I am implementing a board. And I want the View that the users can include links.

Now, my TextView has


attribute. However, It works a bit differently from what I expected.

enter image description here

As you can see, even though I click the empty area(+lines in the picture), the TextView is detecting the link(blue highlight) and If I let my finger go, a web browser opens and it goes to the URL.

That's not what I want. I just want it to work when I ONLY click the link. (Not other non-URL text nor empty area)


  1. I DON'T KNOW what users write, In other words, I can't preset specific links nor guess what URLs would be. It must be applied to all the links even that I don't know.

  2. The number of links can be more than ONE. And I don't know how many links would be included in the user's content.

Somebody tells me I need to make it to HTML using WebView. Well, Do I really just have that option? because I don't think the chat rooms of many messengers such as Kakaotalk, Whatsapp, Wechat, or Telegram, even Instagram are made of WebView.

What if I want to place pictures in the middle of text? I guess it is impossible with TextView. How to make it?



You need to handle links programatically. This way you will have way to separate clickable spans and non-clickable spans. And you can add more code on click of spans if you want.

Below is the way which you can use

private void createUrlSpansInTextView(TextView tv, String text) {
  tv.setText(text); // Or you can remove this line if you already set text to textview
  SpannableString current=(SpannableString)tv.getText();
  URLSpan[] spans=
      current.getSpans(0, current.length(), URLSpan.class);

  for (URLSpan span : spans) {
    int start=current.getSpanStart(span);
    int end=current.getSpanEnd(span);

    current.setSpan(new CustomURLSpan(span.getURL()), start, end,

Where CustomURLSpan is

private static class CustomURLSpan extends URLSpan {
  public CustomURLSpan(String url) {

  public void onClick(View widget) {
      // Write your code to load urls

What if I want to place pictures in the middle of text? I guess it is impossible with TextView. How to make it?

No with TextView not possible. You need to look for third party libraries. I am not sure if any exists.