Reputation: 4090
I am implementing a board. And I want the View that the users can include links.
Now, my TextView has
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.
What if I want to place pictures in the middle of text? I guess it is impossible with TextView. How to make it?
Upvotes: 0
Views: 226
Reputation: 83018
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
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.
Upvotes: 1