Disable option on soft-keyboard android programmatically - android

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

Related

How does the home button or volume up/down button works (Android)? Does pressing this button generates a hardware interrupt?

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.

How to use Onkeydown Event for Buttons?

I am using a KeyEvent in my App.Now I have to replace the KeyEvent With the Button in my Layout.I dont't know how to use OnclickListener with KeyEvent.I searched & got a Method using OnTouchListener.https://stackoverflow.com/a/13311997/2781359
I tried it.I don't know how to use it ???
Now I Want to make the keyEvent Action when the button is Clicked..
My Current Method
public boolean onKeyDown(int keyCode, KeyEvent event)
{
int unicode = event.getUnicodeChar();
if (unicode == 0)
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_VOLUME_UP:
unicode = KeyboardAction.UNICODE_ARROW_UP;
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
unicode = KeyboardAction.UNICODE_ARROW_DOWN;
break;
}
if (unicode != 0)
{
this.application.sendAction(new KeyboardAction(unicode));
}
return super.onKeyDown(keyCode, event);
}
So Help me in the Right Direction :)
Thanks for your Help ...
EDIT
When I Press the Volume UP Button the following action happens
unicode = KeyboardAction.UNICODE_ARROW_UP;
But i Want to Perform this action With a normal button(using OnClickListener) in my layout .
Do you mean you want a button to act as if one of the buttons has been pressed? I did this in my app to simulate the back button being pressed with the following code:
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
Trigger back-button functionality on button click in Android

Disable android system volume overlay

Is there a way to disable/override the volume overlay/toast ? (its not actually a real toast)
I want to replace it, I already wrote a system Overlay which acts like the original, plus there are more sliders.
Now I just want to disable the original (image below) so it is not displayed.
You can try to override the following function to diable the volume buttons:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean result;
switch( event.getKeyCode() ) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
result = true;
break;
default:
result= super.dispatchKeyEvent(event);
break;
}
return result;
}

Overriding back button press from Activity's subClass in Android

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").

Can Menu and onKeyDown work together?

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

Categories

Resources