0

I am trying to open my WebView app from URLs from browser and Google Search.

I tried using the following code as suggested here: How to launch app on click of url in android

    <intent-filter>
        <data android:scheme="https" />
        <data android:scheme="https" android:host="www.webviewurl.com"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

But it didn't work.

I want to force open all URLs like:

https://*.webviewurl.com/*
http://webviewurl.com/*

to https://www.webviewurl.com/* in the WebView.

I've already done this part in the server using .htaccess.

I want the URLs to open in WebView by default.

Maroof Mandal
  • 489
  • 9
  • 28

2 Answers2

2

you can use WebViewClient to perform this task.

 mWebView = (WebView) findViewById(R.id.activity_main_webview);

    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://chatadda.com");

    mWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        String url2="http://www.chatadda.com/";
         // all links  with in ur site will be open inside the webview 
         //links that start ur domain example(http://www.chatadda.com/)
        if (url != null && url.startsWith(url2)){
            return false;
            } 
       // all links that points outside the site will be open in a normal android browser
      else  {
            view.getContext().startActivity(
            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
            }
    }
});
Devendra Singh
  • 2,899
  • 4
  • 26
  • 47
-1

The issue has been resolved by doing the following:

AndroidManifest.xml

            <intent-filter>
                <data android:scheme="https" />
                <data android:scheme="https" android:host="www.webviewurl.com"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

MainActivity.java

public class webclient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.contains("play.google")){
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
            if ((url.contains("webviewurl.com"))) {
                view.loadUrl(url);
                return true;
            } else {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
            return true;
        }

Which is working fine. Only problem is, all the pages are opening the homepage in app.

Like if an user clicks on a link say https://www.website.com/folder/page from browser, it opens https://www.website.com/ in the app.

How do I fix this issue?

Maroof Mandal
  • 489
  • 9
  • 28