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.
Related
I have seen many solutions to this problem, but the solutions were for older versions. Is there any way to disable the system buttons Back, Home and Menu for one activity? I am programming for android 7 and above.
You can try override onKeyDown.This can intercept back button and do something you want.Also override onBackPressed() can intercept back button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
do something;
return true;
}
return false;
}
But the menu button and home button I'm not sure if still can intercept in now days.
You can try to lock the com.android.systemui package
I'm trying to solve this but couldn't find any answer on SO. My issue is that whenever I'm in my app and I press the home button, my app layout becomes for a second like the wallpaper for my menu on the phone!
Something like this :
I've tried to override the menu button like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
this.finish();
}
return super.onKeyDown(keyCode, event);
}
But this will not work. Any help will be appreciated.
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 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
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.