WebView onKeyListener not working - android

I have to intercept the touch on the screen, and this is my code:
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(URL);
mWebView.setOnTouchListener(this);
mWebView.setOnKeyListener(this);
and my listeners:
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Log.i(TAG, "Hello, touch.");
onTouchEvent();
return false;
}
return false;
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent)
{
Log.i(TAG, "Hello, onKey.");
onTouchEvent();
return false;
}
While the on touch is working fine for touches on the screen, it dosen't intercept the touch event on the keyboard. For this reason, I've added the onkeylistener, but it's not intercepting the key events. As last thing, I don't care about what is typed, but just if the screen has been touched.

why not trying onkeyDown?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
OR
override this method --> onUnhandledKeyEvent (WebView view,
KeyEvent event)
more details on:
https://developer.android.com/reference/android/webkit/WebViewClient.html#onUnhandledKeyEvent(android.webkit.WebView,%20android.view.KeyEvent)
OR
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean dispatchFirst = super.dispatchKeyEvent(event);
// Listening here for whatever key events you need
if (event.getAction() == KeyEvent.ACTION_UP)
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_SPACE:
case KeyEvent.KEYCODE_ENTER:
// e.g. get space and enter events here
break;
}
return dispatchFirst;
}
NOTE:-
Preventing developers from accessing the events by default was made by Googlers on purpose. Because the key event input isn't the only one anymore. There're gestures, voice and more is coming. Official recommendation is to "stop relying on legacy key events for text entry at all". Check out more details here: https://code.google.com/p/android/issues/detail?id=42904#c15

With my recent experience of inexplicable and inconsistent behaviour of calling loadUrl() before setWebViewClient() I would try calling loadUrl() AFTER setting ANYTHING to do with a multi-threaded WebView. That includes setOnTouchListener() and setOnKeyListener().
I realise my answer might not help your specific problem - but it might help others who have experienced similar behaviour and are setting things before before calling loadUrl().

If you want to detect keypress then try to override
onKeyPreIme(int keyCode, KeyEvent event);

Related

How to obtain the currently selected View onKeyDown method?

I'm trying find if a specific View is selected following a key press. For this purpose I was trying to use the onKeyDown method on my main activity.
I already tried to set a setOnKeyListener on the fragment but it wasn't registering any input.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
// Check if it's selecting a specific View
}
return super.onKeyDown(keyCode, event);
}
Any idea how I could do this?
one from many approach that may use, fun n silly maybe, but it was work for me
on Global declaration
Integer iLastView=0;
on View that selected before
iLastView=iView;
and last in the inKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
if(iLastView==iView1){
//something here
}else if(check){.....}
}
return super.onKeyDown(keyCode, event);
}

How to trigger an Onkeydown event on a WebView

I have an Android apllication which has a Webview Controller and i want to trigger an Onkeydown event to this Webview programatically from the wrapping activity.
How do I do this?
thanks in advance
kobi
A WebView doesn't seem to send click events to an OnClickListener. If you want to achieve this behavior you can use either OnTouchListener or warp the webView in some component and process the click events there..
You can override a Webviews on TouchEvent like this. :)
wv = new WebView(getActivity()){
#Override
public boolean onTouchEvent(MotionEvent event) {
td.onTouchEvent(event);
return super.onTouchEvent(event);
}
};
And you can hand of the oNKeyDown event from the wrapping Activity to the WebView.
Try out as below:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

Disable DPAD keys in android

I am trying to catch events generated by the arrow keys (UP, DOWN, RIGHT and LEFT) and disable them. Below code snippet is from one of the activity class.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.KEYCODE_DPAD_DOWN) return true;
else return true;
}
However, with those code in place, key navigation is working. I tried adding key listener to activity which doesn't work either.
The target device is Samsung GT-I5500 with Android 2.2 version on.
Am I missing anything?
Override onKeyDown also and return true and not false.
Somnething like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
return false;
}
In the documentation, it is stated that you should return:
true if you handled the event
false if if you want to allow the event to be handled by the next receiver.
Your method is returning false, so you are passing the event to the default key handler

onKeyUp doesn't work for KEYCODE_DPAD_CENTER

I have 2 activities, where I overrided the onKeyUp event:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, "Key up: " + keyCode);
return super.onKeyUp(keyCode, event);
}
The first is the main activity of the application, the other one is secondary. The first works fine, but the second captures events for Left-Right-Up-Down but not for Center click. Why?
Since you have a Gallery in your layout, take a look at: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Gallery.java#L1216 . It might be possible that the Gallery steals your DPAD_CENTER event (and also ENTER).
I suggest you to use Activity.dispatchKeyEvent(android.view.KeyEvent) to stop the event before it even goes to that Gallery.
Try using it like this (in your Activity):
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
// Do your stuff here.
return true; // Consume the event (or not, your call)
}
return super.dispatchKeyEvent(event);
}

Android: onKeyListener simulating two instances of a pressed key [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
public boolean onKey() called twice?
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
solveExpression();
return true;
}
return false;
}
});
I'm trying to solve the expression contained within the Display(EditText), by pressing the enter button on the keyboard, yet it always interprets it as though I pressed the button twice. Does anyone know why this happens?
Try...
Display.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
// Check for ACTION_DOWN only...
if (KeyEvent.ACTION_DOWN == event.getAction()) {
solveExpression();
return true;
}
}
}
});
The 'action' can be ACTION_DOWN, ACTION_UP or ACTION_MULTIPLE (the last being for when a key is pressed and held). onKey() will be called for any/all of those actions.
As the other answer mentions, it's triggering twice because it's once for down and once for up.
if (event.getAction()!=KeyEvent.ACTION_DOWN) // we catch only key down events
return true;
Thus you stop listening other keyevents as onClick.
If you want nobody else further in chain to get the event for another piece of work, you should set
return false;
not an android guy either but the fact that it registers twice makes me think that OnKey encompasses a onKeyDown and onKeyUp. Would listening to onKeyUp work for you as well?

Categories

Resources