83

I want my EditText to display a keyboard that ONLY has numbers visible, no other characters.

I have tested with all available inputs and it doesn't work. I searched for a way to get a keyboard that only has numbers but I have only seen references to:

android: inputType = "numberPassword"

But I want the numbers to be visible, like this: (numberPassword)

enter image description here

I have tried with:

android:digits="0123456789"
android:inputType="phone"

and

android:inputType="number"

but it appears like this:

enter image description here

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Jorge B.
  • 1,144
  • 2
  • 17
  • 37

14 Answers14

141

After several tries, I got it! I'm setting the keyboard values programmatically like this:

myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

Or if you want you can edit the XML like so:

android:inputType="numberPassword"

Both configs will display password bullets, so we need to create a custom ClickableSpan class:

private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return source;
    }
}

Finally we need to implement it on the EditText in order to display the characters typed.

myEditText.setTransformationMethod(new NumericKeyBoardTransformationMethod());

This is how my keyboard looks like now:

Boken
  • 4,825
  • 10
  • 32
  • 42
Mauricio Sartori
  • 2,533
  • 6
  • 26
  • 28
28

android:inputType="number" or android:inputType="phone". You can keep this. You will get the keyboard containing numbers. For further details on different types of keyboard, check this link.

I think it is possible only if you create your own soft keyboard. Or try this android:inputType="number|textVisiblePassword. But it still shows other characters. Besides you can keep android:digits="0123456789" to allow only numbers in your edittext. Or if you still want the same as in image, try combining two or more features with | separator and check your luck, but as per my knowledge you have to create your own keypad to get exactly like that..

Narkha
  • 1,197
  • 2
  • 12
  • 30
Kanth
  • 6,681
  • 3
  • 29
  • 41
  • 1
    does not works, appears with characters: "." "-", "" and i want a keyboar like image. – Jorge B. Dec 11 '12 at 09:59
  • 1
    I think it is possible only if you create your own soft keyboard. Or try this android:inputType="number|textVisiblePassword. But it still shows other characters. Besides you can keep android:digits="0123456789" to allow only numbers in your edittext. Or if you still want the same as in image, try combining two or more features with | separator and check your luck, but as per my knowledge you have to create your own keypad to get exactly like that.. – Kanth Dec 11 '12 at 10:15
  • What if my editText appears in the dialog, and thus, I only declare it in .java code? i.e. the dialog editText does not appear in XML file. How can I still achieve this? – Sibbs Gambling Jun 20 '13 at 07:40
  • 1
    @perfectionming Well, actually you can set this feature to edittext programatically in java code too like this --> editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL); or whatever input type you want, you keep just with the help of the constants. For more constant values, refer the above link in my answer. If it helped, don't forget to upvote my answer ;) or you can even inflate the layout through xml code for your custom dialog and then set these input feature in the xml code itself.. – Kanth Jun 20 '13 at 08:31
  • THanks! but if I set this feature to edittext programatically in java code, the pop-up keyboard still contains letters. i.e. I can still enter letter. How can I make a PURE number keyboard pop up in this case? – Sibbs Gambling Jun 20 '13 at 08:34
  • 1
    @perfectionming I don't think it would show letters to enter as it is purely based on api. Let me check and get back or else you could show me the image or code of what your present scenario is. – Kanth Jun 20 '13 at 08:42
  • 1
    @perfectionming It worked on my side even on custom dialog when I keep input feature through java code. – Kanth Jun 20 '13 at 09:15
10

If you use Kotlin :

XML

android:inputType="numberPassword"

Code

editText.transformationMethod = null

result:

enter image description here

Mori
  • 2,653
  • 18
  • 24
9
<EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

I have tried every thing now try this one it shows other characters but you cant enter in the editText

edit.setRawInputType(Configuration.KEYBOARD_12KEY);

enter image description here

Mudassar Shaheen
  • 1,397
  • 1
  • 9
  • 15
  • thanks for the help, but don't solve my problem. I already tryed that way. I need numeric keyboard. And your example show another's characters. I'm trying everything but it's not easy. :( – Jorge B. Dec 12 '12 at 10:43
  • Obviously irrelevant to the question, but where'd you get that theme? – Kayla Jan 12 '15 at 02:49
4

If you want to show just numbers without characters, put this line of code inside your XML file android:inputType="number". The output:

enter image description here

If you want to show a number keyboard that also shows characters, put android:inputType="phone" on your XML. The output (with characters):

with characters

And if you want to show a number keyboard that masks your input just like a password, put android:inputType="numberPassword". The output:

enter image description here

I'm really sorry if I only post the links of the screenshot, I want to do research on how to do really post images here but it might consume my time so here it is. I hope my post can help other people. Yes, my answer is duplicate with other answers posted here but to save other people's time that they might need to run their code before seeing the output, my post might save you some time.

Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
Jeanne vie
  • 518
  • 6
  • 12
3

I think you used somehow the right way to show the number only on the keyboard so better try the given line with xml in your edit text and it will work perfectly so here the code is-

  android:inputType="number"

In case any doubt you can again ask to me i'll try to completely sort out your problem. Thanks

Ravi
  • 2,277
  • 3
  • 22
  • 37
  • thanks, but doesn't works, appears with characters: "." "-" "" see the images in the question please. – Jorge B. Dec 11 '12 at 10:21
  • 1
    Yes Jorge just wait i am trying to make a keyboard which will show only number not any special character – Ravi Dec 11 '12 at 11:04
  • why donot you check the validation in program whether the user entered number or special character ,if he entered number than proceed otherwise not – Ravi Dec 11 '12 at 11:08
  • Because the specification of the app that I'm doing is with keyboard númeric only, without another characters. if not a spec I would have solved the problem already... I keep trying and looking on google and here... :( – Jorge B. Dec 11 '12 at 11:57
  • 1
    i am trying if i got success to make that i 'll let you know – Ravi Dec 12 '12 at 03:55
  • What if my editText appears in the dialog, and thus, I only declare it in .java code? i.e. the dialog editText does not appear in XML file. How can I still achieve this? – Sibbs Gambling Jun 20 '13 at 07:40
3

A little late but it might help somebody else.

You can set the keylistener for your EditText and pass to it only the keys to listen for:

yourEditText.keyListener = DigitsKeyListener.getInstance("0123456789")
Jorge B.
  • 1,144
  • 2
  • 17
  • 37
Raul Lucaciu
  • 132
  • 7
2

There is absolutely no need to implement a new transformation method. This alone solves it pretty slick:

XML

android:inputType="numberPassword"

Code

textField.transformationMethod = null
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
themenace
  • 2,601
  • 2
  • 20
  • 33
  • Not working - still accepts commas, dots etc (depend of localization and keyboard) at least on api 31 – Fedir Tsapana Sep 29 '21 at 10:43
  • @FedirTsapana I highly doubt that. I've tested this method on the latest API and on a variety of devices and APIs. It only allows inputs from 0-9. Additionally it shows the DELETE key and an ENTER key. – themenace Sep 29 '21 at 11:02
1

In xml use this

android:inputType="numberPassword"

Then use this in code

mobileET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
Soni Kumar
  • 283
  • 1
  • 4
  • 16
1

If you want this in Jetpack Compose:

TextField(
     value = textState,
     onValueChange = { text ->
         textState = text
     },
     keyboardOptions = KeyboardOptions.Default.copy(
         keyboardType = KeyboardType.NumberPassword
     ),
     visualTransformation = VisualTransformation.None
)
skafle
  • 627
  • 1
  • 6
  • 16
0

Place the below lines in your <EditText>:

android:digits="0123456789"
android:inputType="phone"
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • OP specifically wants to exclude `,*#` but this answer includes those characters. – Josh Correia Jul 18 '20 at 23:38
  • @JoshCorreia it is a correct answer, the first line doesn't allow those symbols but show them on the soft keyboard anyway, it is the equivalent of DigitsKeyListener.getInstance("0123456789") as key listener – Alireza Jamali Jan 25 '23 at 13:24
0

Koltin solution:

I made an extension in Koltin to achieve this. I tried many ways even with filters but there are some bugs with the Filter like repeating the same text while typing.

fun EditText.onAfterTextChanged(allowedChars: String, listener: (String) -> Unit) {
    addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            val input = editable.toString()
            val newValue = replaceInvalidCharacters(input, allowedChars)
            if (newValue != input) {
                setText(newValue)
                setSelection(text.length)
            }
            listener(newValue)
        }

        override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }
    })
}

fun replaceInvalidCharacters(value: String, allowedChars: String): String {
    var finalValue = value.replace("\\p{So}+".toRegex(), "") // To remove emojis when copy paste
    if (finalValue.isNotEmpty()) {
        val lastChar = finalValue.last()
        if (!allowedChars.contains(lastChar, false)) {
            finalValue = finalValue.dropLast(1)
        }
    }
    return finalValue
}

You can use it in your Activity like below:

editText.onAfterTextChanged(getString(R.string.supported_digits)) { // Here you can pass whatever the digits you want to allow
            val length = it.length
            if (length > 250) { // You can also check how much length to allow
                return@onAfterTextChanged
            }
        }
  • You can pass whatever the characters you want to allow to it
  • It will filter emojis while typing
  • It will remove the emojis even if you copy-paste
  • It will allow only the chars which are present in the allowedChars
  • It works with all the android versions
  • It will solve the issue of repeating text with filters
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
-3

For the EditText if we specify,

android:inputType="number"

only numbers can be got. But if you use,

android:inputType="phone"

along with the numbers it can accept special characters like ;,/". etc.

Irfan
  • 2,713
  • 3
  • 29
  • 43
Akanksha Hegde
  • 1,738
  • 11
  • 14
-3

In xml edittext:

android:id="@+id/text"

In program:

EditText text=(EditText) findViewById(R.id.text);
text.setRawInputType(Configuration.KEYBOARDHIDDEN_YES);
Narkha
  • 1,197
  • 2
  • 12
  • 30
adrian
  • 1