Ethan Allen
Ethan Allen

Reputation: 14835

Is there a way to get an Android soft keyboard that has only numbers (no decimals, spaces) with Java code?

I have an iPhone app that uses different keyboard layouts. Some are custom and some are built in:

Numbers only:

Numbers only

Decimals:

Decimals

Custom X button for ISBN numbers:

Custom X button for ISBN numbers

I'd like to do the same thing on Android, but even the normal InputType.TYPE_CLASS_NUMBER still includes a decimals, space, comma, etc.

Android

How can I customize the keyboards in Android??

Upvotes: 3

Views: 8581

Answers (5)

TopherC
TopherC

Reputation: 79

Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:

    android:inputType="numberPassword"
    android:digits="0123456789"
    android:singleLine="true"
    android:ems="4"
    android:textColor="@android:color/black"
    android:gravity="center"

Upvotes: 0

Pier Betos
Pier Betos

Reputation: 1048

There is a way. Use

numericField.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL|InputType.TYPE_CLASS_NUMBER);

It anticipates number pad only on Holo, and backward compatible number pad on lower versions.

Upvotes: 0

Gautam
Gautam

Reputation: 4026

I think this question has already been answered on StackOverflow. Look at this:

Only show number buttons on Soft Keyboard in Android?

@twaddington is correct that what you are asking for won't be possible with the built-in keyboard. One thing you can do to prevent non-digits from being entered is set the following in XML for your EditText.

android:inputType="phone"
android:digits="1234567890"

If you want to this in code, and not in XML, I think this should work:

numericField.setInputType(Configuration.KEYBOARD_12KEY);
numericField.setKeyListener(new DigitsKeyListener());

To develop your own numeric soft keyboard, this tutorial may help.

Upvotes: 4

Vishesh Chandra
Vishesh Chandra

Reputation: 7071

Please use inputType = "number", then you will get only number keypad.

<EditText android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

I hope it will help you. :)

Upvotes: 0

twaddington
twaddington

Reputation: 11645

It's not possible using the built-in keyboard. You'll have to develop a custom soft keyboard, or write an inline view that works like a keyboard replacement.

Upvotes: 3

Related Questions