Android webview opens page in default browser instead of my webview

android, browser, webview

Solution

You need to implement `setWebViewClient(....)` like so.

webView.setWebViewClient(new WebViewClient() {

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

Update:

Make your Activity like so

public class MainActivity extends Activity {
WebView browser;

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

    // find the WebView by name in the main.xml of step 2
    browser=(WebView)findViewById(R.id.wvwMain);

    // Enable javascript
    browser.getSettings().setJavaScriptEnabled(true);  

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

    browser.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
                }
        });
     // Load the webpage
    browser.loadUrl("http://news.google.com/");
   }
}

Problem

I am trying for two days to find a code to work with what I have.. I am following this tutorial: http://techvalleyprojects.blogspot.ro/2011_08_01_archive.html I have the following problem: The webpage I want to open in my app is not loaded inside my webview, instead it opens in the default browser. How can I modify my code so that all links open in the webview. I need a easy solution because I am very new to Android.. Thank you. ``` package com.example.name; import android.app.Activity; import android.os.Bundle; import android.webkit.WebChromeClient; import android.webkit.WebView; public class MainActivity extends Activity { WebView browser; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // find the WebView by name in the main.xml of step 2 browser=(WebView)findViewById(R.id.wvwMain); // Enable javascript browser.getSettings().setJavaScriptEnabled(true); // Set WebView client browser.setWebChromeClient(new WebChromeClient()); // Load the webpage browser.loadUrl("http://news.google.com/"); } } ```

Original source

Related problems