3

Is there any way to programmatically tell android to open the keyboard when the focus is obtained by an EditText?

Also is there any way to tell it to open the numeric keyboard?

Thanks Victor

Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
  • possible duplicate of [Open soft keyboard programmatically](http://stackoverflow.com/questions/5593053/open-soft-keyboard-programmatically) – Vincent Cantin Apr 30 '14 at 10:18

3 Answers3

9

To pop up a numeric keyboard on start of the activity you can follow these steps:

Created edit text field in layout as: (Not needed if you want a qwerty keyboard)

 <EditText
        ...
        android:inputType="number"    
        ...  />

In function onCreate() show soft keyboard

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

Most important is to give focus to edit text in onResume method.

    @Override
    public void onResume() {
        super.onResume();
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }
Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
  • I tried this code . When the Activity loads, I can see the focus on the EditText but I can not see the numeric keypad until I click on the EditText. Am I missing something. – j10 May 28 '17 at 05:46
  • The EditText will take the focus but you have to force keypad to come up. You can write that part in onCreate(). – Rana Ranvijay Singh May 28 '17 at 08:56
8

to make it numeric, use this

text.setInputType(InputType.TYPE_CLASS_NUMBER);

and as far as I know, the keyboard will pop up when needed automatically

dldnh
  • 8,923
  • 3
  • 40
  • 52
5

To show the keyboard:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(viewToEdit, 0);

To hide the keyboard:

if (getCurrentFocus() != null) {
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
}
Kai
  • 15,284
  • 6
  • 51
  • 82
  • This works in general, however I didn't mention that my EditText is in a Dialog, and as soon as I bring up the dialog, the keyboard hides. If I bring up the dialog first, the keyboard doesn't show - any ideas? – Victor Grazi Mar 21 '12 at 02:55