40

On the soft keyboard in Android you can set the soft keyboard to show the numbers instead of a-z keyboard using android:inputType="numberDecimal". However, what do I do if I only want to show the top number row 1 2 3 4 5 6 7 8 9 0 and not the following rows starting with @ # $ % ...?

Thanx for listening!

Benny Skogberg
  • 10,431
  • 11
  • 53
  • 83
  • You cab see my answer on below question http://stackoverflow.com/questions/39593324/cannot-resolve-symbol-showsoftinput/39593871#39593871 – Android Help Sep 20 '16 at 12:21
  • @AndroidHelp The question was posted six years ago, and it was solved four years ago. Today I don't develop apps for Android. I've moved on to work with SharePoint :) – Benny Skogberg Sep 20 '16 at 12:57
  • isnt there an answer to this yet? Like an inbuilt keyboard by android yet? – Prashanth VG Jan 17 '17 at 02:34
  • it's depend on the third-part keyboard app thats user use you can only tell the softkey what type of data you want to using android:inputType – alacoo Jan 27 '19 at 00:11

9 Answers9

67
android:inputType="phone"
android:digits="1234567890"

is an option

softarn
  • 5,327
  • 3
  • 40
  • 54
43

You must only add this line on your code:

input.setRawInputType(Configuration.KEYBOARD_12KEY);

this will show only the numeric keyboard.

sergidk
  • 546
  • 5
  • 4
11

The phone number pad is the closest thing I've found (set inputType="phone" on your EditText).

Justin
  • 20,509
  • 6
  • 47
  • 58
9

The accepted answer did not work for me (tested on OnePlus 3t and Samsung Galaxy S6/S7 phones).

This solution uses numberPassword but overrides the default transformation method for the EditText to show characters instead of showing dots.

<EditText
    android:id="@+id/userid"
    android:inputType="numberPassword"
    android:maxLength="6"
/>

// Numeric 6 character user id
EditText input = findViewById(R.id.userid);

// Process input and show characters instead of dots
input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
PDP
  • 111
  • 1
  • 4
3

Just posted an answer here but re-posting for simplicity:

Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:

    android:inputType="numberPassword"
    android:digits="0123456789"
    android:singleLine="true"
    android:ems="4"
    android:textColor="@android:color/black"
    android:gravity="center"
Community
  • 1
  • 1
TopherC
  • 79
  • 3
  • 1
    This makes the text turn into a password, hiding the previously entered numbers. – Elliot Blackburn Jan 01 '16 at 20:08
  • Quite so. I don't know why G doesn't offer the 10-key to be shown without the other characters but maybe for some developers, this is a good way to produce the reduced keyboard. Of course, if people don't mind a few extraneous characters, the inputType of "number" will do the trick. For a 10-key without the password style visibility, a custom keyboard is the solution. – TopherC Jan 03 '16 at 00:03
2

The accepted answer didn't work for me. It kept showing other unwanted characters

I found a way to accomplish the behavior I wanted by changing the inputType to numberPassword and then disabling the password dots

textInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
textInput.transformationMethod = SingleLineTransformationMethod()
sgc_code
  • 485
  • 2
  • 7
1

Last I looked into it, Android did not have any good options for that. I ended up having to write my own version of a soft keyboard-like user interface.

Spike Williams
  • 35,795
  • 13
  • 48
  • 60
  • So in order to go for the lowest version used in the market as of today (1.5) - I need to write my own SoftKeyboard.java class then. That'll be a challange :-) – Benny Skogberg Jul 28 '10 at 21:10
  • Could you please tell me how you did that? Coz I need exactly that as well. – keinabel Dec 05 '16 at 16:00
0

I had implemented this for android in Xamarin. So, my code is in C#. But the principal stays the same. You can set attribute of edittext to android:inputType="numberPassword".

Then within your code you attach custom transformation method to your edittext view.

holder.edtxtQty.TransformationMethod = new HiddenPasswordTransformationMethod();

private class HiddenPasswordTransformationMethod : global::Android.Text.Method.PasswordTransformationMethod
        {
            public override Java.Lang.ICharSequence GetTransformationFormatted(Java.Lang.ICharSequence source, View view)
            {
                return new PasswordCharSequence(source);
            }
        }

    private class PasswordCharSequence : Java.Lang.Object, Java.Lang.ICharSequence
    {
        private char DOT = '\u2022';

        private Java.Lang.ICharSequence _source;
        public PasswordCharSequence(Java.Lang.ICharSequence source)
        {
            _source = source;
        }

        public char CharAt(int index)
        {
            return _source.CharAt(index);
        }

        public int Length()
        {
            return _source.Length();
        }

        public Java.Lang.ICharSequence SubSequenceFormatted(int start, int end)
        {
            return _source.SubSequenceFormatted(start, end); // Return default
        }

        public IEnumerator<char> GetEnumerator()
        {
            return _source.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _source.GetEnumerator();
        }
    }
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
0

From code:

et.setInputType(InputType.TYPE_CLASS_NUMBER)
Oz Shabat
  • 1,434
  • 17
  • 16