Androidguy1
Androidguy1

Reputation: 11

How can I make internal links load within webview?

I am having issues with trying to get internal links to open within my webview-based android client that I am doing for a small business. What do I need to do so that the application can only open links with "https://forums.mywebsite.com" and "https://mywebsite.com"?

I already have the procedure I need to open external links using the action view, but this only links away from the first page that is stated in the loadurl(), and does not link them within the client, and treats them as external links.

aWebView.setWebViewClient(new WebViewClient() {
                                          @Override
                                          @TargetApi(21)
                                          public boolean shouldOverrideUrlLoading(WebView aWebView, WebResourceRequest request) {
                                              Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());

                                              superWebView.getContext().startActivity(intent);

                                              return true;
                                          }
                                      });

TL;DR: link expectations are not working, and any help would be appreciated.

Upvotes: 1

Views: 332

Answers (1)

Mohamed Abdalkader
Mohamed Abdalkader

Reputation: 469

As the docs explains:

returning true causes the current WebView to abort loading the URL, while returning false causes the WebView to continue loading the URL as usual.

You should have logic in the method that checks the URL and returns false only if the url matches your expected URLs and starts the activity and return true otherwise.

Something along those lines:

    WebView.setWebViewClient(new WebViewClient() {
        @Override
        @TargetApi(21)
        public boolean shouldOverrideUrlLoading(WebView aWebView, WebResourceRequest request) {
            if (TextUtils.equals(request.getUrl().toString(), "https://forums.mywebsite.com")
                    ||TextUtils.equals(request.getUrl().toString(), "https://mywebsite.com")) {
                return false;
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
                superWebView.getContext().startActivity(intent);
                return true;
            }
        }
    });

Upvotes: 1

Related Questions