Anthony Massie
Anthony Massie

Reputation: 139

How to open Android soft keys with onclick listener?

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?

Upvotes: 0

Views: 816

Answers (3)

Buzz
Buzz

Reputation: 516

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

android:inputType="number"

that should force the keyboard to be numeric.

Upvotes: 0

Clayton Oliveira
Clayton Oliveira

Reputation: 643

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

Upvotes: 3

Anton Kovalyov
Anton Kovalyov

Reputation: 932

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);

Upvotes: 0

Related Questions