Android - Enabling only URL link, but disabling the others when it touches something non-URL link in TextView
Android - Enabling only URL link, but disabling the others when it touches something non-URL link in TextView
I am implementing a board. And I want the View that the users can include links.
Now, my TextView has
android:autoLink="web"
android:autoLink="web"
attribute. However, It works a bit differently from what I expected.

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)
and
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.
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.
HTML
What if I want to place pictures in the middle of text? I guess it is impossible with TextView. How to make it?
1 Answer
1
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.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL()), start, end,
0);
}
}
Where CustomURLSpan is
CustomURLSpan
private static class CustomURLSpan extends URLSpan {
public CustomURLSpan(String url) {
super(url);
}
@Override
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks. But this didn't work for me...
– Chanjung Kim
Jun 30 at 22:11