I created an Activity that has a title and a web view in a LinearLayout. In the onResume() method it calls webView.loadUrl(url). The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. What I want to see is the page being shown in the WebView below the title. What could be the problem?

Edit: Ok, did some further search and found this one:

Clicking URLs opens default browser

It points to the WebView tutorial here.

Just implement the web client and set it.

Solution 1

Answering my question based on the suggestions from Maudicus and Hit.

Check the WebView tutorial here. Just implement the web client and set it before loadUrl. The simplest way is:

myWebView.setWebViewClient(new WebViewClient());

For more advanced processing for the web content, consider the ChromeClient.

Solution 2

Use this:

lWebView.setWebViewClient(new WebViewClient());

Solution 3

use like this:

protected void onCreate(Bundle savedInstanceState) {

    WebView myWebView = (WebView) findViewById(R.id.webView1);
    myWebView.setWebViewClient(new WebViewClient());

Solution 4

Make your Activity like this.

public class MainActivity extends Activity {
WebView browser;

 protected void onCreate(Bundle savedInstanceState) {

    // find the WebView by name in the main.xml of step 2

    // Enable javascript

    // Set WebView client
    browser.setWebChromeClient(new WebChromeClient());

    browser.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return true;
     // Load the webpage

Solution 5

I was facing the same problem and I found the solution Android's official Documentation about WebView

Here is my onCreateView() method and here i used two methods to open the urls

Method 1 is opening url in Browser and

Method 2 is opening url in your desired WebView.
And I am using Method 2 for my Application and this is my code:

public class MainActivity extends Activity {
   private WebView myWebView;

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);

      // Show the dummy content as text in a TextView.
      if (mItem != null) {

         /* Method : 1
          This following line is working fine BUT when we click the menu item then it opens the URL in BROWSER not in WebView */
         //((WebView)   rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);

        // Method : 2
        myWebView = (WebView) rootView.findViewById(R.id.detail_area); // get your WebView form your xml file
        myWebView.setWebViewClient(new WebViewClient()); // set the WebViewClient
        myWebView.loadUrl(mItem.url); // Load your desired url

    return rootView;
}                                                                                               }

Solution 6

Try this code...

private void startWebView(String url) {

    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {      
        ProgressDialog progressDialog;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            return true;

        //Show loader on url load
        public void onLoadResource (final WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(view.getContext());
        public void onPageFinished(WebView view, String url) {
            if (progressDialog.isShowing()) {
                progressDialog = null;
            }catch(Exception exception){


     // Javascript inabled on webview  

    // Other webview options

     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null); 

    //Load url in webview

Solution 7

If you see an empty page, enable JavaScript.

webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();

Solution 8

Simply Answer you can use like this

public class MainActivity extends AppCompatActivity {

     protected void onCreate(Bundle savedInstanceState) {
         WebView webView = new WebView(this);
         webView.setWebViewClient(new WebViewClient());

Solution 9

If you're using webChromeClient I'll suggest you to use webChromeClient and webViewClient together. because webChromeClient does not provides shouldOverrideUrlLoading. It is okay to use both.

        webview.webViewClient = WebViewClient()
        webview.webChromeClient = Callback()

private inner class Callback : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (newProgress == 0) {
                progressBar.visibility = View.VISIBLE
            } else if (newProgress == 100) {
                progressBar.visibility = View.GONE


Solution 10

I just found out that it depends on the formatting of the URL:

My code just uses


no need to set

webView.setWebViewClient(new WebViewClient())

at least in my case. Maybe that's useful for some of you.