error unknown URL scheme android webview

Problem error unknown URL scheme android webview

This issue of getting error unknown URL scheme android webview is very common among developers. The main cause behind this error is when one is trying to add a link to their HTML code. The error that appears on the screen looks as shown below:

 

 

The code for the above error is shown below as a reference:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // This line is to force links and instead of a browser it will open in the webview.
        mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://myexample.com");
    }
}

 

Solution

One of the major cause behind this error is the following link do not have a client associated with it. So to solve this error one needs to add a client to the above code. 

 

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( URLUtil.isNetworkUrl(url) ) {
                return false;
            }
            if (appInstalledOrNot(url)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity( intent );
            } else {
                // do something if app is not installed
            }
            return true;
        }

    });
}

 

After adding the client to run it you need to pass it to intent as well. Another solution for the above problem can be one must override the WebViewClient’s shouldOverrideUrlLoading function so that you may manually control link transmission.

Due to the fact that HTML URLs beginning with mailto: whatsapp: & tg: (Telegram). Since it is uncommon for URLs to begin with “http://” or “https://,” WebView cannot read them correctly; instead, we should utilize intent to reroute them.

An exemplar code for the above concept is:

 

@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                view.getContext().startActivity(intent);
                return true;
            } catch (Exception e) {
                Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
                return true;
            }
        }

 

One of the last solutions can be links to mailto that won’t load in your webview. You must conduct an intent-driven check for it in shouldOverrideUrlLoading. The code snippet for the above program is:

 

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));
        view.reload();
        return true;
    }
  }

 

 

Also Read: Built-in Functions of Python

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *