I want to use Menu and OnkeyDown in same activity but it is not working, when I use OnkeyDown then menu disappear from the activity. I want to make a action using virtual keyboard's enter key. When I press enter key of virtual keyboard one toast should be appear.
Please help.
Thanks in advance
You have to return false outside the switch statement of the onKeyDown but true for every key you handle:
public boolean onKeyDown(int KeyCode, KeyEvent event) {
switch (KeyCode) {
case KeyEvent.KEYCODE_CAMERA:
//take photo
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//lower volume
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
//raise volume
return true;
}
return false;
}
This works for 2.3.3
Related
I want to know that if possible disable hide option on a soft-keyboard programmatically.
Hide Option
//Disable return button
#Override
public boolean dispatchKeyEvent(KeyEvent event){
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
return true;
}
}
return super.dispatchKeyEvent(event);
}
This code disable, as you can see, the return button. For your case you should replace KEYCODE_BACK with another constant. I don't know for sure if it is one for option button but you can check the list right here: https://developer.android.com/reference/android/view/KeyEvent.html
I was wondering how does the home button or volume up/down button works in Android? Does pressing this button generates a hardware interrupt?
I would like to execute a piece of code with higher privilege (in kernel) by pressing this home/volume key. Is it possible? Any pointers?
if pressing these button generates a hardware interrupt, I think I have to modify the interrupt handler to execute the code I want to execute. Is this correct?
Thanks in advance!
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU: // Menu button
doSomething();
return true;
case KeyEvent.KEYCODE_HOME: // Home button
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_UP: // Volume Up key
doSomething();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN: // Volume Down key
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
You can find more keys here.
I have implemented onKeyDown function in my activity and i want to open a dialog if we long press on power button. I have implemented that function as per following manner and working fine on older versions of android but isn't working on android 6.0
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_POWER :
if(event.isLongPress()){
//Do something
return true;
}
break;
default :
break;
}
return super.onKeyDown(keyCode, event) ;
}
Can you please help me.
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
I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").