How to restrict TextView to allow only alpha-numeric characters in Android - android

I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!

In the XML, put this:
android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA
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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});

The InputFilter solution works well, and gives you full control to filter out input at a finer grain level than android:digits. The filter() method should return null if all characters are valid, or a CharSequence of only the valid characters if some characters are invalid. If multiple characters are copied and pasted in, and some are invalid, only the valid characters should be kept (#AchJ's solution will reject the entire paste if any characters a invalid).
public static class AlphaNumericInputFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// Only keep characters that are alphanumeric
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (Character.isLetterOrDigit(c)) {
builder.append(c);
}
}
// If all characters are valid, return null, otherwise only return the filtered characters
boolean allCharactersValid = (builder.length() == end - start);
return allCharactersValid ? null : builder.toString();
}
}
Also, when setting your InputFilter, you must make sure not to overwrite other InputFilters set on your EditText; these could be set in XML, like android:maxLength. You must also consider the order that the InputFilters are set. When used in conjunction with a length filter, your custom filter should be inserted before the length filter, that way pasted text applies the custom filter before the length filter (#AchJ's solution will overwrite all other InputFilters and only apply the custom one).
// Apply the filters to control the input (alphanumeric)
ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(editText.getFilters()));
curInputFilters.add(0, new AlphaNumericInputFilter());
InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
editText.setFilters(newInputFilters);

This should work:
textView.setInputType(InputType.TYPE_CLASS_NUMBER);

Related

Is there a way to create a new input type in android edit text?

I wanted to know if there was a way to create your own input type, which will be usable in your edit text.
Like for example I want to force the user to only put numbers between 1 and 5, so the input type "number" doesn't feet with what I want, and I would like to create a new input type which would be "number_1_5" and only allow the user to add a number between 1 and 5.
I searched in the android docs, and on the web but found nothing :(
Thank you for your help!
Use from InputFilter for this purpose:
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++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("[12345]*");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
Log.d("", "invalid");
return "";
}
}
return null;
}
};
mEditText.setFilters(new InputFilter[]{filter});

Edittext is not accepting number as input type

I cannot enter number (numeric) in the Edittext field.
If I keep android:inputType="text", number cannot be entered.
If I keep android:inputType="text|number", the keyboard accepts only digits.
But If I keep android:inputType="textMultiLine", I can enter both text/number but the first character cannot be number and it should be a character.
And I tried doing with other options too, nothing worked. I'm building the application with the target sdk:21
Note: I need both text / number as input
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))) {
return "";
}
}
return null;
}
};
your_edit_text.setFilters(new InputFilter[]{filter});
public boolean isLeadingDigit(final String value){
final char c = value.charAt(0);
return (c >= '0' && c <= '9');
}
You can use
android:inputType="numberDecimal"
Try to not specify inputType or set it to "none" if you want to be able to enter text and numbers.

Limit languages in Android/iOS for input text

Is there any way to limit the input text of edittext to english and hebrew only?
Since data is transferred to a server SQL DB, and I do not want to store other languages....
So is it a way to limit to specific language input?
Or maybe there is other way to handle these situation so server will not crash...
Yoav
You can create a custom InputFilter and set it as a filter for your EditText. There's more info about how to do this in this thread. Here's an adaptation of what's there:
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 (!isEnglishOrHebrew(source.charAt(i))) {
return "";
}
}
return null;
}
private boolean isEnglishOrHebrew(char c) {
. . .
}
};
edit.setFilters(new InputFilter[]{filter});

Android: input type for only non-numeric chars

I have an EditText on which I would like to let user enter only non-numeric chars (say A-Z or a-z): is there a way to do it? All the combinations I used (text, textPersonName and so on) let the user select also numbers.
I think you have to write your own InputFilter and add it to the set of filters for the EditText. Something like this might work:
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.isLetter(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});

How to check the value entered in EditText is aplhanumeric or not?

My application takes userid from user as input, the userid is alphanumeric i.e just the first character is (a-z), other part is numeric. How can I validate input of this type ( like G34555) ?
Use a regex. This should do it assuming the first letter can be upper or lower case:
Pattern p = Pattern.compile("[a-zA-Z][0-9]+");
Matcher m = p.matcher("some text you want");
boolean isAlphaNum = m.matches();
http://osdir.com/ml/Android-Developers/2009-11/msg02501.html seems like a more decent solution, it does not allow entering the chars that are not accepted.
Code from link:
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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
I have resolved issue by using simple string function matches
String str="mystring";
str.matches("[a-zA-Z][0-9]+");

Categories

Resources