I am developing an app in which i am using custom list tab,By using this custom list tab i am rendering some child activities, and some of the child activities are having some soft key pad functionality now the problem is when i click on the back hard key(when the soft key pad is on) it is simply killing the activity with out firing OnkeyDown() and onBackpress() events and it is firing onDestroy() event method and this issue is specially occurring in HTC Devices. And the other devices(samsung) are working as exppect i.e. hiding the soft key pas on pressing hard back key. how to this issue?
Thanks,
Ram.
You can try overriding onKeyDown and onKeyUp like this:
#Override
public boolean onKeyDown( int keyCode, KeyEvent event )
{
if ( keyCode == KeyEvent.KEYCODE_BACK )
{
return true;
}
return super.onKeyDown( keyCode, event );
}
#Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if ( keyCode == KeyEvent.KEYCODE_BACK )
{
onBackPressed();
return true;
}
return super.onKeyUp( keyCode, event );
}
I remember reading this to be a problem in Android versions earlier than 2.1
Related
How do I disable the hardware buttons from the samsung devices. Beside the home-button which is a normal button, they have touch-sensitive menu and back button.
The onKeyDown and onKeyUp are not called. I implemented this in the activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(this.getClass().getName(), keyCode + "");
Log.d(this.getClass().getName(), event.KEYCODE_MENU + "");
if (keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
return super.onKeyDown(keyCode, event);
}
It works for the volume buttons, but not this touch-buttons.
Thank you
Check this: Upgraded to AppCompat v22.1.0 and now onKeyDown and onKeyUp are not triggered when menu key is pressed. It solved my problem that was exactly like yours, it seems that new version of AppCompat caused onKeyDow method to not be called anymore.
Also, you may try to update to v23: https://code.google.com/p/android/issues/detail?id=159795#c31 if it's already available. It worked here.
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 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.
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.
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