After clicking link in textview, how to open that link in webview instead of default browser?
The following problems need to be solved:
Linkify the TextView Find a way to listen to a click on a link in the TextView Get the url of the clicked link and load it in the WebView Optional: make the TextView clickable without losing the ability to select text Optional: handle formatted text in the TextView (different text sizes and styles)
1 Linkify the TextView
String text = "These are some sample links:\nwww.google.com\nwww.facebook.com\nwww.yahoo.com";
Spannable spannable = new SpannableString( Html.fromHtml(text) );
Linkify.addLinks(spannable, Linkify.WEB_URLS);
2 + #3 Listen to clicks on links and open them in the WebView
URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan urlSpan : spans) {
LinkSpan linkSpan = new LinkSpan(urlSpan.getURL());
int spanStart = spannable.getSpanStart(urlSpan);
int spanEnd = spannable.getSpanEnd(urlSpan);
spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.removeSpan(urlSpan);
}
For opening in new activity:
private class LinkSpan extends URLSpan {
private LinkSpan(String url) {
super(url);
}
@Override
public void onClick(View view) {
String url = getURL();
if (url != null) {
startActivity(new Intent(LinkTestActivity.this,WebViewActivity.class).putExtra("url",url));
}
}
}
And loding the url in webview.
for more see below link :-
Open URL in WebView instead of default Browser