4

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:

  WebView forumView=(WebView)findViewById(R.id.forumView);
  forumView.getSettings().setJavaScriptEnabled(true);  
  forumView.loadUrl("url");

As i said i am very new to WebView my code might be wrong, please help me to solve this problem.

Thanks in Advance, Rajapandian

Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
Rajapandian
  • 9,257
  • 24
  • 74
  • 86

2 Answers2

4

This piece of code will help you.

wbb = (WebView) findViewById(R.id.webView_tobe_loaded);

    WebSettings wbset=wbb.getSettings();
    wbset.setJavaScriptEnabled(true);
    wbb.setWebViewClient(new MyWebViewClient());
    String url="http://www.google.com";

    System.out.println(getdeviceid());
    wbb.getSettings().setJavaScriptEnabled(true);
    wbb.loadUrl(url);
Sreedev
  • 6,563
  • 5
  • 43
  • 66
3

You'll have to intercept the clicks yourself if you don't want the default Android behavior.

You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.

You set the WebViewClient of your WebView using the setWebViewClient() method.

If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
hcpl
  • 17,382
  • 7
  • 72
  • 73