Calc
Calc

Reputation: 535

Android EditText force numeric keyboard but allow non-numeric chars

I need to maintain an EditText which can be inputted by the user numbers, at some cases I need to set the text of the EditText to something like 12$. So I need to be able to setText() to anything I want.

I've tried setting up an EditText with input type set to number which yields the numeric keypad and also remove the InputFilter's of that EditText - Which still does not allow me to put non numeric chars

input.setFilters(new InputFilter[] {});

Am I doing something wrong? Is there a different way to accomplish my goal, an EditText with a numeric keypad that i can programmatically insert some non numeric chars into?

Upvotes: 8

Views: 5074

Answers (3)

Aviran
Aviran

Reputation: 5458

This will keep it numeric but will allow the $ character, add any other special chars you need to the android:digits attribute

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:digits="0123456789,$">

Upvotes: 12

MisterJimson
MisterJimson

Reputation: 539

Just to add to aviran's answer. If you put any "," in the string then you will also allow commas in the EditText.

Below will only allow 0-9 and $.

<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:digits="0123456789$">

Upvotes: 2

Bhaskar
Bhaskar

Reputation: 949

Set input type Phone in xml.

This could help you...

how to set only numeric value for edittext in android?

Upvotes: 0

Related Questions