4

In my application, I am trying to display the numeric keypad when the user clicks on a button.

When the button is clicked, I shift the focus to the EditText in my layout using requestFocus() and next I need to display the numeric keypad so that the user can type in the values..

The values will always be numeric and hence I need to show only the numeric keypad.

I tired using this inside my button's onClick() method but it does not work.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Please provide me with any solution to this.

Also, my application is for an Android tablet supporting 4.0.3.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
Swayam
  • 16,294
  • 14
  • 64
  • 102

2 Answers2

6

in EditText put below line.

android:inputType="number"
Nikhil
  • 16,194
  • 20
  • 64
  • 81
  • It doesn't work in tablets. Basically it allows numbers only however the shown keypad is alphanumeric even though non-numbers are disabled.. – Gunhan Sep 18 '13 at 13:00
6

This one in your EditText property

android:inputType="phone" (This will displayed phone numeric keypad)

or

android:inputType="number" (This will displayed numeric keypad)

Now, you have to just set Focus on your EditText on Button's click..

something like,

edtNumber = (EditText) findViewById(R.id.number);

// Button's onClick....
@Override
 public void onClick(DialogInterface dialog, int which)
  {
    edtNumber.requestFocus();
  }
user370305
  • 108,599
  • 23
  • 164
  • 151
  • It works only with the problem that the keypad is alphaNumeric. The editText takes the input as only numbers but it just looks bad. Plus there are other things in the keypad that I dont need, things like "tab", "(",")"..etc etc. Is there any way to remove all these and display only the numbers ? – Swayam Jun 21 '12 at 09:24
  • If you write android:digits="1234567890" then only number can allowed, You can't enter other characters. – user370305 Jun 21 '12 at 09:30
  • How do I control the buttons displayed on the virtual keyboard ? – Swayam Jun 21 '12 at 09:35
  • Sorry, for that. You have to make your own KeyBoard, in android there is not any method or specific keyboard. – user370305 Jun 21 '12 at 09:36
  • 1
    Look at [An EditText for entering IP addresses](http://kmansoft.com/2011/02/27/an-edittext-for-entering-ip-addresses/) and [How to develop a soft keyboard for Android?](http://stackoverflow.com/questions/3480715/how-to-develop-a-soft-keyboard-for-android) – user370305 Jun 21 '12 at 09:47
  • Yeah, looks like I can make my own keyboard from there.Thanks a lot for your help. – Swayam Jun 21 '12 at 09:48
  • 1
    Welcome Buddy..! also similar question http://stackoverflow.com/questions/3357302/only-show-number-buttons-on-soft-keyboard-in-android and this one https://github.com/chrisboyle/sgtpuzzles/blob/master/src/name/boyle/chris/sgtpuzzles/SmallKeyboard.java – user370305 Jun 21 '12 at 09:49