0

I have a listview with textfilterenabled. On some devices the keyboard shows when you click long on the menu button, but on some devices it's standard not possible to display the keyboard.

Does anybody know how to show the keyboard on a long click on the menu button. I have already a code but it's not working. The keyboard doesn't show up.

My code:

@Override
public boolean onKeyLongPress(int keycode, KeyEvent event){         
    if (keycode == KeyEvent.KEYCODE_MENU){              
        InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(lv, InputMethodManager.SHOW_IMPLICIT);             
    }
    return true;
}

Thanks in advance!

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Simon
  • 2,328
  • 6
  • 26
  • 30

2 Answers2

0

You need to focus the keyboard on a certain editText

so:

EditText etHello= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etHello, InputMethodManager.SHOW_IMPLICIT);
TheBlueCat
  • 1,147
  • 5
  • 19
  • 38
0

You call the showSoftInput()-method with the SHOW_IMPLICIT-flat. It's documentation says:

Flag for showSoftInput(View, int) to indicate that this is an implicit request to show the input window, not as the result of a direct request by the user. The window may not be shown in this case.

Since you want the keyboard to be shown when the user holds down the menu-button, it's not implicit anymore. Try passing in 0 for the flat-parameter.

It's also possible to toggle the keyboard, which seems to work for other people, too. For this, refer to this older question: android - show soft keyboard on demand


The problem is in your onKeyLongPress()-method. To work with the menu button, some extra work is needed. See my answer to this older question: How can I create a long touch event on the physical menu button?

Last but not least, you should be aware that the menu-button has been deprecated and is not present on newer devices. For compatibility reasons, you get an emulated version of it, but the ActionBar (and it's search-field) should be favored over legacy support. So you might want to check for an alternative.

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • I tried it but it's not about the show implicit. When I make a button with an onclicklistener and use this code: InputMethodManager inputMethodManager (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);iinputMethodManager.showSoftInput(lv, InputMethodManager.SHOW_IMPLICIT); It shows me the keyboard. So I think the problem is the longclick on the menu button thing. And on new Android devices the menu button is standard not shown but when an application needs it, the menu button is shown – Simon Aug 05 '12 at 15:18
  • @user1380611 I updated my answer. Check if this works for you. – Lukas Knuth Aug 05 '12 at 17:21
  • Thanks! I just found that too in this article: http://mobile.tutsplus.com/tutorials/android/android-sdk-intercepting-physical-key-events/. I try tomorrow if it works! – Simon Aug 05 '12 at 21:10