6

I'm writing an Android game that runs in fullscreen landscape mode, and has buttons placed at the bottom left and bottom right of the window. The problem is that one of these buttons is (on many phones) right next to the Menu button, so the player might accidentally press Menu instead.

If it is pressed briefly, I simply pause the game and show the in-game menu. No problem there.

But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen. Since it gets in the way, and is completely useless in this Activity, I would like to disable it.

I tried the following approaches.

Via InputMethodManager

From: Hide soft keyboard on activity without any keyboard operations

Since I have only one view (a GLSurfaceView) I tried this in my Activity.onCreate():

InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);

It doesn't work: the soft keyboard still comes up on Menu long-press.

Via the AndroidManifest.xml

From: How to stop the android soft keyboard from ever coming up in my entire application

I added this to my manifest:

<activity 
    android:windowSoftInputMode="stateAlwaysHidden"
>

Does a great deal of nothing as well.

So... is there even a way? How?

Community
  • 1
  • 1
Thomas
  • 174,939
  • 50
  • 355
  • 478

4 Answers4

12

Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    // From the docs:
    // "Note that in order to receive this callback, someone in the event [chain]
    // must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Override default handling, and don't pop up the soft keyboard.
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        openOptionsMenu();
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

But it feels like a hack, so I'm hoping that someone comes up with a better solution.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • I am for think out of the box and all but sometimes you just want it to do a certain thing and you code helps accomplish exactly that. I don't know if I would call it a hack, probably feels that way because you have to just through some hoops to get it to work... Thanks! – bytebender Apr 19 '11 at 16:14
  • 1
    I agree it's a hack - but it's a hack that works gloriously well. Thank you very much; it's just what I needed (none of the other approaches worked for me, either). – Carl Manaster Jul 28 '11 at 21:32
3

But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen.

What phone do you have? Are you sure? I've never once seen that happen and I just tried it and it doesn't work on my phone.

Also, that sounds like a user problem. Don't try to subvert the user. If the user REALLY wants to open a keyboard in your app, you should let them and if it's useless, they'll hit back and it will go away.

A more concerning issue should be that your buttons are so close to the menu buttons.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • Good thinking-outside-the-box answer, thanks. The phone is a Nexus One. My buttons are necessarily close to the edges of the screen, to keep as much screen estate as possible for the real action. The fact that the "hardware" buttons are no more than an extension of the touch screen, without a tangible boundary, might be considered a design flaw in the phone... but I don't think the N1 is the only phone with this design. – Thomas Jan 14 '11 at 14:06
  • @Thomas: Actually there's software to prevent accidental menu button presses. Have you had this issue personally? I've almost never accidentally hit my menu buttons. On an original moto droid. – Falmarri Jan 14 '11 at 18:20
  • Not me personally, but someone who never used an N1 before. Maybe it's not as big an issue as I originally thought. – Thomas Jan 14 '11 at 20:12
  • I think there are cases where you do want to prevent the user from opening a keyboard in your app. A camera app showing preview frames is one example. The DroidX opens the soft keyboard by default on long press of menu. – Error 454 Jan 31 '11 at 18:59
0

Try using hideSoftInputFromWindow() instead. According to the documentation:

request to hide the soft input window from the context of the window that is currently accepting input.

gulbrandr
  • 1,005
  • 1
  • 9
  • 16
0

use android:windowSoftInputMode="adjustPan" in android manifest. I think this is best choice to prevent the view goes up.

Manjeet
  • 25
  • 1