I have an EditText, where I don't want to allow any special characters since I am storing this String in the SQLlite database. Right now I use:
<item name="android:digits">abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</item>
This works fine for English. But when I switch my keyboard to a different language (Japanese, Russian, Hindi, etc), I am unable to enter anything. What is the right way to do this?
Alternatively:
I am using a ContentProvider to access my SQLite database. So I have methods reading/writing from and to database use
context.getContentResolver().insert(uri, values);
context.getContentResolver().delete(uri, values);
context.getContentResolver().query(uri, values); //for a special case, but only uses id=? in the query
context.getContentResolver().update(uri, values);
So do I need to escape the special characters at all?
//try to add this fillter
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("[a-zA-Z0-9?]*")) {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString("");
return sp;
} else {
return "";
}
}
}
return null;
}
};
edtiText.setFilters(filters);
I know that is a little late to answer to your question but maybe this helps somebody in a future.
What you need is a filter like Haresh's answer but modifing the regular expresion like:
private InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence src, int start, int end, Spanned dest, int dstart, int dend) {
if(src.toString().matches("[\\p{Alnum}]*")){
return src;
}
return "";
}
};
If you don't want numbers in the editText then change
"[\\p{Alnum}]*"
with this
"[\\p{Alpha}]*"
If you need something more specific take a look to this link:
PD: I used this code in my application that supports english, spanish and french language and it's working OK.
I hope this can help somebody!
Related
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});
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);
I need to set validation for a edittext view should allow two numeric values and two decimal values.
example: 22.32
Please let me know how to do this validation
Thanks in advance
Try this out. I suck at regex so it may not be the best, but give it a try.
EditText text = new EditText(this);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("^\\d(\\d(\\.\\d{0,2})?)?")) {
return "";
}
}
return null;
}
};
text.setFilters(filters);
boolean isValid=<EditText Variable>.getText().toString().matches("\\d{2}\\.\\d{2}");
Put this method in a onClickListener and I guess you will be good to go.
I want to restrict capital letter typing through keyboard on my form.
any one guide me how to achieve this?
In source code:
InputFilter smallFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isUpperCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v).toLowerCase();
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
TextUtils
.copySpansFrom((Spanned) source, start, end, null, sp, 0);
return sp;
} else {
return s;
}
}
}
return null;
}
};
EditText vText = ...;
vText.setFilters(new InputFilter[]{smallFilter});
It is based on Android source of: InputFilter.AllCaps. Tested and works.
First, what's your goal? Why do you want to restrict capital letters? Can't you take care of them on the validation of user input and use toLowerCase()
If you do need to restrict capital letters, the only way I can think of is overriding onTextChanged(CharSequence text, int start, int before, int after) and trying to do whatever you want inside it. I haven't tested it, but it might work.
Update: radek mentioned InputFilter. That solutions seems cleaner than mine, but I've never used them.
InputFilters can be attached to Editables to constrain the changes that can be made to them.
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]+");