Mick
Mick

Reputation: 7947

"implement a WebViewClient class on the WebView"

I am attempting to do some operations with a WebView within an activity and was wondering about how to detect when a web page had loaded (this was in relation to an ad serving system). I emailed the very slow ad-agency tech support team, who eventually replied "You can use HTML banners inside a WebView and you will need to implement a WebViewClient class on the WebView". I wasn't 100% sure what this meant, but my first guess was to add the words "implements WebViewClient" to my actibvity, i.e.:

public class MyActivity extends Activity implements WebViewClient
{

I was then fully expecting eclipse to tell me that I had some missing imports which it would then give me the option of adding... but to my surprise, the two options were "create interface WebViweClient" and "Fix project setup". So now I'm confused, not sure what to do next.

Can anyone show me some example code or give me a hint to set me off in the right direction?

EDIT: What I'm attempting to do is load up an ad on a webview, and then at some later time (and having checked that the ad-load was completed), I'll display the webview.

Upvotes: 0

Views: 2973

Answers (2)

Carnal
Carnal

Reputation: 22064

WebViewClient is a class not an interface.

You will have to create your own class like MyWebViewClient extends WebViewClient and in your Activity you can set the WebViewClient to your WebView, like this:

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

Upvotes: 1

Talha
Talha

Reputation: 12717

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but  that URL should open in the same screen.
 */
public class WebViewClientDemoActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web = (WebView) findViewById(R.id.webview01);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://www.google.com");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
  {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
        web.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
   }
}

Upvotes: 2

Related Questions