How do you set the EditText keyboard to only consist of numbers on Android?

AndroidAndroid LayoutAndroid Edittext

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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:

![][1]

[1]: http://i.stack.imgur.com/8gYx4.png "tooltip"

Solution 2 - Android

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..

Solution 3 - Android

<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

Solution 4 - Android

If you use Kotlin :

XML

android:inputType="numberPassword"

Code

editText.transformationMethod = null

result:

enter image description here

Solution 5 - Android

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.

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

a lil 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")

Solution 9 - Android

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

Solution 10 - Android

Place the below lines in your <EditText>:

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

Solution 11 - Android

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.

Solution 12 - Android

In xml edittext:

android:id="@+id/text"

In program:

EditText text=(EditText) findViewById(R.id.text);
text.setRawInputType(Configuration.KEYBOARDHIDDEN_YES);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJorge B.View Question on Stackoverflow
Solution 1 - AndroidMauricio SartoriView Answer on Stackoverflow
Solution 2 - AndroidKanthView Answer on Stackoverflow
Solution 3 - AndroidMudassar ShaheenView Answer on Stackoverflow
Solution 4 - AndroidMoriView Answer on Stackoverflow
Solution 5 - AndroidJeanne vieView Answer on Stackoverflow
Solution 6 - AndroidRaviView Answer on Stackoverflow
Solution 7 - AndroidthemenaceView Answer on Stackoverflow
Solution 8 - AndroidRaul LucaciuView Answer on Stackoverflow
Solution 9 - AndroidShailendra MaddaView Answer on Stackoverflow
Solution 10 - AndroidAvadhani YView Answer on Stackoverflow
Solution 11 - AndroidAkanksha HegdeView Answer on Stackoverflow
Solution 12 - AndroidadrianView Answer on Stackoverflow