0

I have a View that has a button. On the button click, I want the soft keyboard to pop up with numbers. I can use toggleSoftInput, however, I see no way to change the input type.

public class TimerOnClickListener implements View.OnClickListener {
    @Override
    public void onClick( View view ) {
        Context context = view.getContext();

        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInput( 0, 0 );
    }
}

How can I toggle the input with numbers only?

Anthony Massie
  • 139
  • 1
  • 9

3 Answers3

3

Following code will display keypad to you:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);

However if you want to force the keyboard that appears into being the Number or telephone keypad, then you have to set the input type on that particular view/widget.

InputType.TYPE_CLASS_NUMBER; 
InputType.TYPE_CLASS_PHONE;

eg.

your_edit_text.setInputType(InputType.TYPE_CLASS_NUMBER);

Credits: Ritesh Gune InputMethodManager show numpad in webview

Community
  • 1
  • 1
Clayton Oliveira
  • 625
  • 1
  • 6
  • 15
0

Check this answer:

set inputType to your EditText to "number"

To show the keyboard:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, 0);
Community
  • 1
  • 1
Anton Kovalyov
  • 932
  • 4
  • 13
0

Make sure you set your view in XML to a number:

android:inputType="number"

that should force the keyboard to be numeric.

Buzz
  • 516
  • 7
  • 21