For my android app I have multiple edittexts on the main screen. If i have the first edittext in focus the menu/back buttons operate fine, if i have any of the other edittexts in focus than neither of them work at all. I'm not doing anything special regarding the menu/back buttons relative to that edittext, i'm not sure what the cause is? Has anyone run into a similar issue and/or know of the cause/solution?
Thanks!
I was have the same problem and I found solution for I was have OnKeyListener that return true; when I change it to return false; the problem was fixed
my_editText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// you can use the next line to ensure back button & menu button will return false
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_MENU) return false;
// any other key you don't want to call other listener for it
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
OnKeyListener.onKey(android.view.View, int, android.view.KeyEvent)
easy fix for me, I had removed the only keyboard app.
just install a keyboard app and the buttons worked again
Related
On a Samsung Tablet, we have the following keyboard:
When a clic occurred on the right bottom arrows, in the view pager, fragment is changed. I want to intercept this event or remove the arrows from the keyboard. Is it possible ?
I tried to intercept keyevent with onKeyUp and onKeyDown methods, but it's not working:
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
return true;
}
My problem is the same as here
Try to use dispatchKeyEvent(KeyEvent event):
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed ", String.valueOf(event.getKeyCode()));
if(event.getKeyCode() == KeyEvent.KEY_A /*select required keycode*/ ){
// perform task
}
return super.dispatchKeyEvent(event);
}
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);
}
I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them
Right now when you hold down the menu button on my android device it pulls up a soft keyboard.Is there a way to override this? I would rather choose what happens on a long touch of this button.
When using onKeyLongPress it only detects when the "Back" button is held down. How can I make this work for the menu button?
For this, you can use the onKeyLongPress()-method, offered by the KeyEvent.Callback-class (can be used in Activity's too, since they are a subclass of the KeyEvent.Callback-class).
There is also a little trick to make this work: You'll have to tell Android to track a long-press click on the "Menu"-button as the onKeyLongPress()-method will not be triggered otherwise. This is done in the normal onKeyDown()-method.
So your code might look like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
// Do what you want...
Toast.makeText(this, "I'm down!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyLongPress(keyCode,event);
}
A great article with further informations can be found on the Android Developer Blog.
Hi Any reply is precious for me and appriciated as well
in one edit text i am setting onKeyListener so that when I enter 5 numerics in edittext it will accept n do next process but in samsung galaxy tablet it is not working i am using these lines of code
zipcode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.out.println("setOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListenersetOnKeyListener");
if (event.getAction() == KeyEvent.ACTION_UP && zipcode.getText().length() == 5) {
System.out.println("OnKeyListener11111");
started = true;
searchByZipcode(zipcode.getText().toString());
}
return false;
}
});
searchByZipcode(zipcode.getText().toString());
line takes the text we are writing in to webservice but in galaxy flow doesnt get into onKeylistner can any1 pls help me out thanks
That's odd. Only in Samsung Galaxys? Is this the full Listener? Maybe you have some other method catching KeyEvents that return true?
Try replacing the code for onKey() by onKeyUp(), which is a TextView method:
http://developer.android.com/reference/android/widget/TextView.html#onKeyUp%28int,%20android.view.KeyEvent%29
BTW, why do you add return false;? Is there any other Listener that requires to handle the KeyEvent? If not, I would suggest to change it for return true;
If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.