r3cc
r3cc

Reputation: 315

How can I open hyperlink in the app instead of browser?

I have a HTML text like this:

val text = "Please click <a href=\"https://google.com\">here</a> for read the message. "

And I set to my TextView.

binding.textLabel.text = Html.fromHtml(text)

However, the link opening in browser instead of the application. I want it to open within the app. How can I do it?

Upvotes: 0

Views: 94

Answers (1)

Narendra_Nath
Narendra_Nath

Reputation: 5173

Do it in two steps

  1. Create a webview activity
  2. Send the URL as a parameter to the webview activity on the link click in `webview.loadUrl(url)

If it still persists use the android:autoLink="" attribute.

If you want further links to open in the webview itself use

ebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});

Upvotes: 1

Related Questions