How to trigger an Onkeydown event on a WebView - android

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);
}

Related

WebView onKeyListener not working

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);

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);
}

How to access menu button onLongClick in Android?

How can I set a listener for long-click performed on hardware Menu button? What is the way of programmatic access to the Menu button?
Moreover how can I distinguish long-click from single-click? (As far as I know when long-click is performed the single-click event is propagated as well - I do not want this to happen because I need 2 different actions for these 2 situations. Long-click and single-click separate listeners for the device Menu button)
Thank you!
This shoule be fairly straight forward. Check out the KeyEvent.Callback on the Android developer's website.
There you will find onKeyLongPress() as well as onKeyDown() and onKeyUp(). This should get you on the right track. Comment or post you code if you need any further help.
Edit: I just re-read the question. If you are having trouble distinguishing single click from long click, you will need to use onKeyDown and onKeyUp and check the duration of the click. Esentially you will start a timer in the onKeyDown and check the time in the onKeyUp. You will have to watch for FLAG_CANCELED.
Further Edit: I found the time to do a couple of tests. This code should do what you want (onKeyUp() gets only short press events and onLongPress() gets only long press events).
The key thing here is in the call to event.startTracking() in the onKeyDown() handler.
Place in Activity (this should also work in a custom view as well but untested):
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Handle Long Press here
Log.i("KeyCheck", "LongPress");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("KeyCheck", "KeyDown" + keyCode);
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking(); //call startTracking() to get LongPress event
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && event.isTracking()
&& !event.isCanceled()) {
// handle regular key press here
Log.i("KeyCheck", "KeyUp");
return true;
}
return super.onKeyUp(keyCode, event);
}

How to keep DropDownList of AutoCompleteTextView opened after pressing the Back key?

i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

Android listen for backbutton longclick

I am overriding the backbutton with a onBackPressed() function
how do I also detect long clicks on the backbutton? Is there an equivalent of #Override onBackLongPressed() ?
This might help you (Check the first comment) - Android long key press
From Android 2.0, Activity contains the method
public boolean onKeyLongPress(int keyCode, KeyEvent event)
For exemple, a long key press on the back button would be :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// do your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Check "Story 2" here. There's not a shortcut for it like there is onBackPressed().
I think you'll have to use onKeyLongPress and handle the KEYCODE_BACK event yourself.

Categories

Resources