0

whey i wrote below code, the url is opening in the default browser, why its not loading in the my app.

setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("http://www.google.com");
umesh
  • 1,095
  • 11
  • 23
  • poss duplicate - http://stackoverflow.com/questions/7305089/android-how-to-load-external-webpage-inside-webview – Neil Jan 06 '13 at 11:20

2 Answers2

1

Try setting a WebViewClient on the WebView:

myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
        return true;
    }
});
myWebView.loadUrl("http://www.google.com");
Jason Robinson
  • 29,432
  • 18
  • 72
  • 128
1

Maybe you want this solution:

WebView webview = findViewById(R.id.webviewLW);
webview = new WebView(this);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

webviewLWComplience.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
        }

        @TargetApi(android.os.Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
            // Redirect to deprecated method, so you can use it in all SDK versions
            onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
        }
    });

webviewLWComplience.loadUrl(CONSTANTS.HOST_URL);
setContentView(webviewLWComplience);

Don't forget to add permission:

<uses-permission android:name="android.permission.INTERNET"/>

Also check this link: https://stackoverflow.com/a/7306176

Ali Azaz Alam
  • 1,348
  • 1
  • 11
  • 23