How to show the numeric keypad
Asked Answered
G

6

24

I read several other posts and using input.setInputType(TYPE_NUMBER_FLAG_DECIMAL); does open the keyboard but its not the numeric keyboard

Is there a trick to this?

Gurolinick answered 13/1, 2011 at 3:55 Comment(0)
M
26

give android:inputType="number" inside your xml file

Edit: "Number" does not work, changed to "number" (lowercase N)

Mellophone answered 13/1, 2011 at 4:3 Comment(1)
The editview is on a popup using builder so there is no xml. I think the setInputType does the same thing as doing that in the XMLGurolinick
A
14

a) in xml

android:numeric="decimal"

b) in code

EditText editView = new EditText(this);
editView.setKeyListener(new DigitsKeyListener());
Abscind answered 9/2, 2011 at 6:33 Comment(0)
S
9

In your xml, you have to do this:

android:inputType="Number"

In your code, do:

editText.requestFocus();

Then:

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

Took me many days to figure this out. I even tried editText.performClick(); Not working.

Spawn answered 26/9, 2011 at 3:32 Comment(1)
The flag InputMethodManager.SHOW_IMPLICIT is the key to open the corresponding type of keyboard setted in the EditText InputType. Thanks!Nickelodeon
A
3

To pop up a numeric keyboard on start of the activity i used following steps:

Created edit text field in layout as:

 <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();
    }
Antimonyl answered 6/5, 2014 at 6:14 Comment(0)
S
0

I found two easy ways to do it, add one of them to EditText block in XML file:

  1. Numeric dial keyboard pad
        android:inputType="phone"
        android:digits="1234567890"
  1. Numeric section of keyboard
        android:inputType="number"
Snips answered 16/5, 2021 at 12:20 Comment(0)
I
0

Here's how to do it totally programmatically. As you need to show the numeric keyboard, you should technically set the inputType as a number inside the XML layout. But since you don't have the XML layout, you need to setup the corresponding flags programmatically. Here's what the documentation says about the number flag:

<!-- A numeric only field.  Corresponds to
{@link android.text.InputType#TYPE_CLASS_NUMBER} |
{@link android.text.InputType#TYPE_NUMBER_VARIATION_NORMAL}. -->
<flag name="number" value="0x00000002" />

so in order to have the same behaviour you need to do what's below:

mEditText.setInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL );
Ideograph answered 3/7 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.