How to set chrome version of webview as default in WebView

Issue

I’ve tried this code which works perfectly but my website opens in its default browser. I want to change it to chrome because my website is more compatible with chrome & here I see broken links. How can I do it?

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView  (WebView) findViewById(R.id.webview);
        mywebView.setWebViewClient(new WebViewClient());
        mywebView.loadUrl("https://******.com/");
        WebSettings webSettings  mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    public class mywebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onBackPressed() {
        if(mywebView.canGoBack()) {
            mywebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

Solution

WebView is a widget that you place in the UI of one of your activities. It is not the default browser.

you are calling loadUrl() on the WebView, if it is bringing up the default browser, that is because the Web server is issuing a redirect. If you run the application now with a site that has a redirect like html5rocks.com, your app ends up opening the site in a browser on the device, not in your WebView — probably not what you expected. This is because of the way the WebView handles navigation events.

Here’s the sequence of events:

1-The WebView tries to load the original URL from the remote server, and gets a redirect to a new URL.

2-The WebView checks if the system can handle a view intent for the URL, if so the system handles the URL navigation, otherwise the WebView will navigate internally (i.e. the user has no browser installed on their device).

3-The system picks the user’s preferred application for handling an http:// URL scheme — that is, the user’s default browser. If you have more than one browser installed, you may see a dialog at this point.

If you’re using a WebView inside an Android application to display some simple web content (for example, a help page), this may be exactly what you want to do. However, for more sophisticated applications, you may want to handle the navigation links yourself.

To handle navigation inside the WebView you need to override the WebView’s WebViewClient, which handles various events generated by the WebView. You already did it like these

 mywebView.setWebViewClient(new WebViewClient());

This is a good step forward, but what if you want to handle links for your site only, while opening other URLs in a browser?

To achieve this you need to extend the WebViewClient class and implement the shouldOverrideUrlLoading method. This method is called whenever the WebView tries to navigate to a different URL. If it returns false, the WebView opens the URL itself. (The default implementation always returns false, which is why it works in your code.)

now you know how to make user see all the pages on your app but if you want the webview look like chrome and use chrome java script engine you can use Chrome Custom Tabs to open an chrome tab on your app Chrome Custom Tabs allow an app to customize how Chrome looks and feels. An app can change things like:

Toolbar color Enter and exit animations Add custom actions to the Chrome toolbar, overflow menu and bottom toolbar The WebView is good solution if you are hosting your own content inside your app. If your app directs people to URLs outside your domain, i recommend that you use Chrome Custom Tabs A complete example is available at https://github.com/GoogleChrome/custom-tabs-client. It contains re-usable classes to customize the UI,

Answered By – Sideeg MoHammed

Leave a Comment