If you want to override some methods, you need to create your own WebView class, which extends WebView .
It will look something like this:
public class CustomWebView extends WebView { public CustomWebView(Context context) { this(context, null); } public CustomWebView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } }
For this to work, you must modify your XML layout file accordingly:
<com.example.stackoverflow.CustomWebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" />
In addition, when you pump up the WebView , make sure you throw it on the correct type, which is equal to CustomWebView .
CustomWebView webView = (CustomWebView) findViewById(R.id.webview);
Otherwise, you will get java.lang.ClassCastException .
jenzz source share