To show a numeric keyboard for an Android EditText, set its android:inputType to number. Android then requests a keyboard layout suited to entering digits. Additional values such as numberDecimal and numberSigned can be used when the field must accept decimal or negative numbers.

Android Numbered Keyboard

Show a Numbers-Only Keyboard for Android EditText

The keyboard appears when the user focuses an EditText. In this Android Tutorial, you will learn how to request a numeric keyboard in XML and Kotlin, select the correct numeric input type, and validate the entered value.

Use android:inputType=”number” for Integer Input

For an age, quantity, count, PIN, or another field that contains digits, use android:inputType="number". This is more appropriate than phone for ordinary numeric values because a phone keyboard may also provide symbols used in telephone numbers.

</>
Copy
<EditText
    android:id="@+id/ageEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter your age"
    android:inputType="number" />

The exact keyboard layout can differ between keyboard applications and Android devices. The input type is a request to the installed input method rather than a guarantee that every keyboard will display identical keys.

Complete Kotlin Android EditText Number Keyboard Example

Create an Android Project and add an EditText to the activity layout. The following example reads the number safely when the user taps a button.

activity_main.xml

</>
Copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="24dp">

    <EditText
        android:id="@+id/ageEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your age"
        android:inputType="number"
        android:maxLength="3" />

    <Button
        android:id="@+id/readButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read age" />

</LinearLayout>

MainActivity.kt

</>
Copy
package com.example.numberkeyboard

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val ageEditText = findViewById<EditText>(R.id.ageEditText)
        val readButton = findViewById<Button>(R.id.readButton)

        readButton.setOnClickListener {
            val age = ageEditText.text.toString().toIntOrNull()

            if (age == null) {
                ageEditText.error = "Enter a valid age"
            } else {
                Toast.makeText(this, "Age: $age", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

toIntOrNull() avoids an exception when the field is empty or does not contain a valid integer. Keyboard configuration should therefore be combined with application-level validation.

Using phone inputType

EditText has an attribute called android:inputType. Providing android:inputType with value phone can display numbered keyboard when the EditText gets focus.

Create an Android Project and replace layout (activity_main.xml) and Kotlin file (MainActivity.kt) with the following code.

activity_main.xml

</>
Copy
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Numbered Input Keyboard" />
    <EditText
        android:id="@+id/phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your age"
        android:inputType="phone" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.kt

package com.tutorialkart.keyboardnumberdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Output

Android Keyboard input type phone

Use phone when the field is intended for a telephone number. For an age or quantity, prefer number. Telephone numbers should normally be stored as text rather than converted to an integer because they may contain leading zeros, a plus sign, spaces, or separators.

Android EditText Input Types for Integers, Decimals, and Negative Numbers

Choose the numeric input type that matches the values accepted by the field.

Required inputXML inputTypeExample value
Positive integernumber25
Signed integernumberSigned-25
Positive decimalnumberDecimal12.5
Signed decimalnumberDecimal|numberSigned-12.5
Telephone numberphone+91 98765 43210
Numeric password or PINnumberPasswordMasked digits

For example, the following field requests a keyboard suitable for signed decimal values.

</>
Copy
<EditText
    android:id="@+id/amountEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter an amount"
    android:inputType="numberDecimal|numberSigned" />

Set a Numeric EditText Keyboard Programmatically in Kotlin

You can configure the input type in Kotlin when the field behavior must be selected at runtime.

</>
Copy
import android.text.InputType
import android.widget.EditText

val quantityEditText = findViewById<EditText>(R.id.quantityEditText)
quantityEditText.inputType = InputType.TYPE_CLASS_NUMBER

For signed decimal input, combine the relevant flags.

</>
Copy
amountEditText.inputType =
    InputType.TYPE_CLASS_NUMBER or
    InputType.TYPE_NUMBER_FLAG_DECIMAL or
    InputType.TYPE_NUMBER_FLAG_SIGNED

Restrict Android EditText to Specific Digits

The numeric keyboard improves data entry, but it should not be treated as the only validation layer. Input can also arrive through paste operations, accessibility services, hardware keyboards, or programmatic changes.

When a field must accept only selected characters, the android:digits attribute can be used with a numeric input type. The following example accepts decimal digits only.

</>
Copy
<EditText
    android:id="@+id/pinEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter PIN"
    android:inputType="number"
    android:digits="0123456789"
    android:maxLength="6" />

android:maxLength limits the number of entered characters. It does not validate a numeric range. For example, an age field still needs a check to confirm that the resulting number is within the range accepted by the application.

Validate the EditText Number Instead of Trusting the Keyboard

After reading the text, parse it with a nullable conversion and then apply the field’s business rules.

</>
Copy
val age = ageEditText.text.toString().toIntOrNull()

when {
    age == null -> ageEditText.error = "Enter a valid whole number"
    age !in 1..120 -> ageEditText.error = "Enter an age from 1 to 120"
    else -> {
        // Use the validated age.
    }
}

For decimal input, use toDoubleOrNull() or a decimal type appropriate to the application’s precision requirements. Be aware that decimal separators and accepted formatting may vary by locale.

Configure the Numeric Keyboard Action Button

android:imeOptions controls the action requested for the keyboard’s bottom-right button. For the final field in a form, actionDone is commonly used.

</>
Copy
<EditText
    android:id="@+id/quantityEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:imeOptions="actionDone"
    android:singleLine="true" />

Use actionNext when focus should move to another field. As with the numeric layout, the final appearance of the action button depends on the installed keyboard.

Android EditText Number Keyboard FAQs

How do I make an Android EditText accept only numbers?

Set android:inputType="number" in XML. You can also set InputType.TYPE_CLASS_NUMBER in Kotlin. Parse and validate the submitted text because a numeric keyboard alone is not a complete input-validation mechanism.

What is the difference between number and phone inputType?

number is intended for numeric values such as ages and quantities. phone is intended for telephone numbers and may show additional characters such as a plus sign, star, or hash depending on the keyboard.

How do I allow decimal numbers in an Android EditText?

Use android:inputType="numberDecimal". To support negative decimals as well, use android:inputType="numberDecimal|numberSigned".

Why can pasted text still require validation?

The input type mainly configures editor behavior and requests a suitable keyboard. Text may still come from paste operations, hardware keyboards, accessibility tools, or code. Validate the final value before saving or processing it.

How do I change the Android keyboard from numbers back to letters?

Change the field’s input type to a text type, such as android:inputType="text", or set editText.inputType = InputType.TYPE_CLASS_TEXT in Kotlin. The keyboard normally refreshes when the field regains focus.

Editorial QA Checklist for Android Numeric EditText

  • Confirm that ordinary numeric fields use number instead of phone.
  • Check whether the field requires integer, decimal, signed, telephone, or password input.
  • Verify that empty, pasted, malformed, and out-of-range values are handled safely.
  • Test the numeric keyboard on more than one Android keyboard application when possible.
  • Confirm that imeOptions, focus movement, and validation messages work with the form.