Android
EditText
Number Keyboard
Input Type
Android Development

How do I show the number keyboard on an EditText in android?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

On Android, the keyboard shown for an EditText is controlled mainly by its input type. If you want a numeric keypad, set the field to a numeric input class in XML or in code, and then add flags only for the extra characters you actually want to allow.

Set the Input Type in XML

For most cases, the simplest solution is to configure the view in your layout file.

xml
1<EditText
2    android:id="@+id/ageInput"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="Age"
6    android:inputType="number" />

When the user focuses this field, Android requests a number-oriented keyboard from the current input method editor. For decimal numbers, signed numbers, or both, combine flags:

xml
1<EditText
2    android:id="@+id/priceInput"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:hint="Price"
6    android:inputType="numberDecimal" />
7
8<EditText
9    android:id="@+id/temperatureInput"
10    android:layout_width="match_parent"
11    android:layout_height="wrap_content"
12    android:hint="Temperature"
13    android:inputType="numberSigned|numberDecimal" />

Use phone only when you want a dialer-style keypad for telephone input. It is not the same as numeric data entry.

Set the Input Type Programmatically

Sometimes you need to choose the keyboard based on runtime state. In that case, set the input type in code.

kotlin
1import android.os.Bundle
2import android.text.InputType
3import android.widget.EditText
4import androidx.appcompat.app.AppCompatActivity
5
6class MainActivity : AppCompatActivity() {
7    override fun onCreate(savedInstanceState: Bundle?) {
8        super.onCreate(savedInstanceState)
9        setContentView(R.layout.activity_main)
10
11        val amountInput = findViewById<EditText>(R.id.amountInput)
12        amountInput.inputType =
13            InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
14    }
15}

That code requests a numeric keyboard with a decimal separator. For whole numbers only, use TYPE_CLASS_NUMBER by itself.

If you also want the keyboard to appear immediately, request focus and ask the input method manager to show it. Keyboard visibility is still ultimately controlled by the system and the active keyboard app, but this is the standard approach:

kotlin
amountInput.requestFocus()

Understand What Input Type Can and Cannot Guarantee

inputType is a hint to the keyboard, not a hard security boundary. Different keyboard apps can render slightly different layouts, and some still allow copy-paste of characters that do not match your intended format.

That means you should validate the text as well, especially for money, identifiers, and values sent to a server.

kotlin
1val raw = amountInput.text.toString()
2val amount = raw.toDoubleOrNull()
3
4if (amount == null) {
5    amountInput.error = "Enter a valid number"
6}

If the accepted character set is narrow, android:digits can help restrict entry further:

xml
1<EditText
2    android:id="@+id/pinInput"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:inputType="numberPassword"
6    android:digits="0123456789" />

This is especially useful for PIN codes, OTP fields, and fixed-format numeric strings.

Pick the Right Numeric Variant

Not every numeric field should use the same keyboard hint. Some common choices are:

  • 'number for integers such as age or quantity'
  • 'numberDecimal for prices, percentages, and measurements'
  • 'numberSigned|numberDecimal for values such as temperatures'
  • 'numberPassword for PIN entry where digits should be obscured'

Choosing the narrowest valid input type improves the keyboard layout and reduces validation mistakes before they happen.

Common Pitfalls

  • Using phone for ordinary numeric input. It opens a keypad optimized for phone numbers, not general numbers.
  • Forgetting decimal or signed flags. number alone does not permit decimal separators or negative signs.
  • Assuming the keyboard layout is identical on every device. Input type guides the keyboard, but device and keyboard app still influence the result.
  • Skipping validation. Users can paste unexpected text, so input type should be paired with parsing and error handling.

Summary

  • Use android:inputType="number" for a basic numeric keyboard.
  • Add numberDecimal or numberSigned when the input format requires them.
  • You can set the same behavior in Kotlin with InputType flags.
  • 'inputType influences the keyboard but does not replace validation.'
  • Use digits or server-side checks when the allowed characters must be tightly controlled.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.