1

I was trying to make a very basic program to load the web page on a web view in the onCreate() method of my main activity, but it always asks to me load that url on my inbuilt web browsers. How can I load the web page on a webview?

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100

3 Answers3

1

You can try below code.

public class Main extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("http://www.google.com");
        setContentView(mWebview );

    }

}
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
0

Yes, you are required to load it through WebViewClient

Check and try below example given on WebView doc:

 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://developer.android.com/");
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0
public class WebViewController extends WebViewClient {

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


}

webView.setWebViewClient(new WebViewController());
Rajan
  • 1,069
  • 1
  • 9
  • 17