i have the following problem to resolve in an Android App. I have an editText which has to show only numbers and the the letters 'x' and 'c' when the keyboard is prompted. Is this possible? Thanks for the help!
Asked
Active
Viewed 2,164 times
1
-
check it http://stackoverflow.com/questions/23212439/how-to-restrict-the-edittext-to-accept-only-alphanumeric-characters – Androider Dec 15 '15 at 14:34
3 Answers
1
Sure you can, with filters using InputFilter.
Here a piece of sample code:
InputFilter filter = new InputFilter()
{
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
{
for (int i = start; i < end; i++)
{
if (Character.isDigit(source.charAt(i)) || (source.charAt(i) == 'x') || (source.charAt(i) == 'c'))
{
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });

Dario Cancelliere
- 112
- 8
-
Thanks for the response, but this doesn't work. It shows all the characters and filters whatever the user enters. I need to show (if possible) only the numbers 0-9 and the letters x and c – pabloim1 Dec 15 '15 at 14:29
-
It's not possible, you have to build your own keyboard or using some control fields to achieve this. – Dario Cancelliere Dec 15 '15 at 14:30
1
You have to build your own keyboard or you can restrict input in such a way:
<EditText
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,xc" />

Ksenia
- 3,453
- 7
- 31
- 63
1
try below properties for your EditText
Example :
Alphabet
android:inputType="text" // for alphabet
you can put your own combination of digits
android:digits="0,1,2,3,4,5,6,7,8,9,*,xc" // you can put your own combination of digits
Alphanumeric
android:digits="0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Numeric
input.setRawInputType(Configuration.KEYBOARD_12KEY); // its show only the numeric keyboard.
-
Thanks Shiva, i will probably use this solution. It wasn't what i expected but hiding the characters is not possible, so this is the best i can do. – pabloim1 Dec 15 '15 at 14:51