I would like to know how to make a validation on EditText. For example, I have one EditText that should only accept numeric values. If something other than a numeric value is typed by the user then it should show an alert message (i.e. "please use a numeric value....").
Is there a function available to find out if the entered text is particular type? If possible please include a code snippet.
Rather than make a pop-up I would integrate a hint into the EditText and I would make it so the user could only type numbers into the EditText (android:numeric, android:hint):
<EditText android:layout_height="wrap_content"
android:numeric="integer"
android:hint="#string/numberHint"
android:gravity="left"
android:id="#+id/name"
android:layout_width="wrap_content"
android:maxWidth="60dp"
android:textSize="6pt">
</EditText>
More information is available here:
http://developer.android.com/reference/android/widget/EditText.html
Another way ,
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
Please Go through My Blog post on android input validation [updated].
EDIT:
Which has information on,
What is regular expression
How to validate android edittext input
Online regular expression library
Online regular expression testing tool
Learn how to write regular expression
If you want nice looking validation messages you can use the setError method on the EditText control as I show here: http://blog.donnfelker.com/2011/11/23/android-validation-with-edittext/
The default capabilities for text/checkbox etc validation is poor within android. I have written some supporting classes to fix this. It contains a validator interface, an abstract inplementation,a validationresult class and 2 examples of custom implemented validations.
1 for regular expressions on text and a simple one to check if a checkbox is checked.
Here is the link to my blog containing the sources and a small bit of explaining
Form validation on Android
Related
Is there a way I can specify an input mask to the EditText control in Android?
I want be able to specify something like ### - ## - #### for a Social Security Number. This will cause any invalid input to be rejected automatically (example, I type alphabetical characters instead of numeric digits).
I realize that I can add an OnKeyListener and manually check for validity. But this is tedious and I will have to handle various edge cases.
Try using an InputFilter rather than an OnKeyListener. This means you don't have worry about tracking individual key presses and it will also handle things like pasting into a field which would be painful to handle with an OnKeyListener.
You could have a look at the source of the InputFilter implementations that come with Android to give you a starting point for writing your own.
The easiest way I know to use a mask on EditText in your Android programs in Android Studio is to use MaskedEditText library (GitHub link).
It's a kind of custom EditText with Watcher that allows you to set a hint with different color (if you want it will be available even when user already started to type), mask and it's very easy to use :-)
compile 'ru.egslava:MaskedEditText:1.0.5'
<br.com.sapereaude.maskedEditText.MaskedEditText
android:id="#+id/phone_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:typeface="monospace"
mask:allowed_chars="1234567"
mask:mask="###-##-##"
app:keep_hint="true"
/>
And that is!
You could take a look at the android.telephony.PhoneNumberFormattingTextWatcher class. It masks a phone number text input with the ###-###-#### pattern.
String phoneNumber = "1234567890";
String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890
editTextMobile.setText(text); //set editText text equal to new formatted number
editTextMobile.setSelection(text.length()); //move cursor to end of editText view
Use this in an onKey function for delicious on-the-fly bunny sex with formatted phone numbers.
This site gives a good 2 classes that help with masking that I found quite useful.
You need to use the items in the comments to improve it. But worth adding to your program for ease of masking your EditText views for many masks.
you can use this efect in EditText
When inputtype is used as textPassword, user was able to entire emoji in edit text. If I use both textpassword and textEmailAddress, then passwords are visible. Is there any easy way to achieve this without using textwatcher. The password field can allow other special characters also.
Please note that I am trying to remove emoji by setting the input type. I am not looking for solutions with regular expressions/textwatcher/filters. If the field is not password type I would have used textEmailAddress to avoid emoji option on keyboard
This allow character only a to z and A to Z :
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:inputType="textPassword"/>
Use this in your Password EditText XML file
android:inputType="textPassword|text"
Look more about input methods here
Also, you can use RegularExpression to check whether your password is valid or not.
I need to restrict input type of EditText in Android such a way that user can enter only digits from 0-9 and user can enter one comma(,) in entire input.
Example: 455,67
try using following code, Hope it works :)
android:inputType="text"
android:digits="1234567890,"
A custom filter extends InputFilter could be wrote. You can look over the link.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/text/InputFilter.java
writing this
android:inputType="number"
in XML attributes of edit text will do that for you
I'm trying to choose correct inputType in my adress dialog streetNumber field.
I want to show numeric keyboard first, but then let user also to input alphabetic characters
for some very special cases. Closer to this is inputType datetime,
but this doesn't allow to enter alphabetic characters. So how to set my streetNumber field correctly?
Use android:inputType="textPostalAddress"
The EditText inherits from TextView and shares its input type attributes with it. They can be found here in the official documentation.
Maybe the input type textPostalAddress would be suitable for your need. If not, plenty of other types are available. The XML attribute that allows setting this type is android:inputType="the type you have chosen".
See if this can help you
myEditText.setRawInputType(Configuration.KEYBOARD_QWERTY)
Is there a way I can specify an input mask to the EditText control in Android?
I want be able to specify something like ### - ## - #### for a Social Security Number. This will cause any invalid input to be rejected automatically (example, I type alphabetical characters instead of numeric digits).
I realize that I can add an OnKeyListener and manually check for validity. But this is tedious and I will have to handle various edge cases.
Try using an InputFilter rather than an OnKeyListener. This means you don't have worry about tracking individual key presses and it will also handle things like pasting into a field which would be painful to handle with an OnKeyListener.
You could have a look at the source of the InputFilter implementations that come with Android to give you a starting point for writing your own.
The easiest way I know to use a mask on EditText in your Android programs in Android Studio is to use MaskedEditText library (GitHub link).
It's a kind of custom EditText with Watcher that allows you to set a hint with different color (if you want it will be available even when user already started to type), mask and it's very easy to use :-)
compile 'ru.egslava:MaskedEditText:1.0.5'
<br.com.sapereaude.maskedEditText.MaskedEditText
android:id="#+id/phone_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:typeface="monospace"
mask:allowed_chars="1234567"
mask:mask="###-##-##"
app:keep_hint="true"
/>
And that is!
You could take a look at the android.telephony.PhoneNumberFormattingTextWatcher class. It masks a phone number text input with the ###-###-#### pattern.
String phoneNumber = "1234567890";
String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890
editTextMobile.setText(text); //set editText text equal to new formatted number
editTextMobile.setSelection(text.length()); //move cursor to end of editText view
Use this in an onKey function for delicious on-the-fly bunny sex with formatted phone numbers.
This site gives a good 2 classes that help with masking that I found quite useful.
You need to use the items in the comments to improve it. But worth adding to your program for ease of masking your EditText views for many masks.
you can use this efect in EditText