Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 266
Calc Avatar asked Apr 03 '14 13:04

Calc


2 Answers

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,$">
like image 114
Aviran Avatar answered Nov 18 '22 20:11

Aviran


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$">

like image 35
MisterJimson Avatar answered Nov 18 '22 20:11

MisterJimson