Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : How to set acceptable numbers and characters in EditText?

I have to set acceptable characters "0123456789" and "semicolon" in the EditText. Below is the code I'm using.

android:digits="0123456789;"
android:inputType="number|text

The problem with that implementation is in HTC phones, semicolon can't be entered but in Samsung and Sony Ericsson, semicolon can be entered. Another problem is when I entered semicolon in Samsung and Sony Ericsson, semicolon can't be deleted. Is there any missing property in the above code? Thanks in advance.

like image 813
iamtheexp01 Avatar asked Apr 27 '12 03:04

iamtheexp01


People also ask

How do you edit text only in numbers?

Using phone inputType EditText has an attribute called android:inputType . Providing android:inputType with value phone can display numbered keyboard when the EditText gets focus. Create an Android Project and replace layout (activity_main. xml) and Kotlin file (MainActivity.

How do I change the default value in EditText?

You can use the setHint() to set it, or set it in XML (though you probably don't want that, because the XML doesn't 'know' the name/adress of your user :) ) Show activity on this post. You can use EditText. setText(...) to set the current text of an EditText field.

Is there a way to define a min and max value for EditText in android?

Testing the min value is a different story as we cannot be sure if the user has finished typing or not and therefore cannot decide if we should block or not. Then in Activity set the InputFilterMax and the OnFocusChangeListenerMin to EditText note : You can 2 both min and max in onFocusChangeListener .

How do I get my keyboard to only show numbers on android?

To get the number pad, and tap the special character key in the lower left corner of the keyboard. Then, tap the key just to the left of the spacebar with the numbers 1-4 in a little square.


2 Answers

Android provides an easy way to edit text fields by modifying the layout xml and adding an android:inputType="text". This lets you easily create some basic validations like numbers, decimal, phone or emails. But there's no parameter for alphanumeric (i.e. no special characters). To do this, you need to use an input filter like below and set the fields you want to validate with that filter in code. This input filter

 InputFilter alphaNumericFilter = new InputFilter() {   
     @Override  
     public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)  
     {  
         for (int k = arg1; k < arg2; k++) {   
             if (!Character.isLetterOrDigit(arg0.charAt(k))) {   
             return ""; 
             }   //the first editor deleted this bracket when it is definitely necessary...
         }
         return null;
     }  
 };   
 mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});   
like image 143
Shankar Agarwal Avatar answered Sep 28 '22 11:09

Shankar Agarwal


To limit what the user can enter as they type use a TextWatcher, discussed in this question Android: How can I validate EditText input?.

Better yet: Allow only selected charcters based on regex in an EditText

like image 32
Sam Avatar answered Sep 28 '22 10:09

Sam