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.
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.
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:
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.
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:
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.
If the accepted character set is narrow, android:digits can help restrict entry further:
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:
- '
numberfor integers such as age or quantity' - '
numberDecimalfor prices, percentages, and measurements' - '
numberSigned|numberDecimalfor values such as temperatures' - '
numberPasswordfor 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
phonefor ordinary numeric input. It opens a keypad optimized for phone numbers, not general numbers. - Forgetting decimal or signed flags.
numberalone 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
numberDecimalornumberSignedwhen the input format requires them. - You can set the same behavior in Kotlin with
InputTypeflags. - '
inputTypeinfluences the keyboard but does not replace validation.' - Use
digitsor server-side checks when the allowed characters must be tightly controlled.
Related reading
- How do I show/hide a UIBarButtonItem?
- How do I shuffle an array in Swift?
- How do I size a UITextView to its content?
- How do I solve the INSTALL_FAILED_DEXOPT error?
- How do I sort an NSMutableArray with custom objects in it?
- How do I sort an NSMutableArray with custom objects in it?
- How do I specify that a non-generic Swift type should comply to a protocol?
- How do I start my app when the phone starts on Android?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.