I'm trying to implement an EditText
that limits input to Capital chars only [A-Z0-9] with digits as well.
I started with the InputFilter method from some post.But here I am getting one problem on Samsung Galaxy Tab 2 but not in emulator or Nexus 4.
Problem is like this :
- When I type "A" the text shows as "A" its good
- Now when I type "B" so text should be "AB" but it gives me "AAB" this looks very Strange.
In short it repeats chars
Here's the code I'm working with this code :
public class DemoFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
int dend) {
if (source.equals("")) { // for backspace
return source;
}
if (source.toString().matches("[a-zA-Z0-9 ]*")) // put your constraints
// here
{
return source.toString().toUpperCase();
}
return "";
}
}
XML file code :
<EditText
android:id="@+id/et_licence_plate_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="0"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:maxLength="3"
android:singleLine="true"
android:textSize="18px" >
</EditText>
I'm totally stuck up on this one, so any help here would be greatly appreciated.
try this:
this also allows filtering when filter() method accepts multiple characters at once e.g. pasted text from a clipboard
recently i faced same problem reason of the problem is... if there is a no change in the input string then don't return source string return null, some device doesn't handle this properly that's why characters are repating.
in your code you are returning
don't return this ,
return null;
in place ofreturn source.toString().toUpperCase();
, but it will be a patch fix , it will not handle all scenarios , for all scenario you can use this code.what is happening in this code , there is a regular expression by this we will find all characters except alphabets and digits , now it will replace all characters with empty string, then remaining string will have alphabets and digits.
The following solution also supports the option of an autocomplete keyboard
I have found many bugs in the Android's InputFilter, I am not sure if those are bugs or intended to be so. But definitely it did not meet my requirements. So I chose to use TextWatcher instead of InputFilter
The above code does not allow users to type any special symbol into your EditText. Only capital alphanumeric characters are allowed.
The problem of characters duplication comes from InputFilter bad implementation. Rather return null if replacement should not change:
I've run into the same issue, after fixing it with solutions posted here there was still a remaining issue with keyboards with autocomplete. One solution is to set the inputType as 'visiblePassword' but that's reducing functionality isn't it?
I was able to fix the solution by, when returning a non-null result in the
filter()
method, use the callThis copies the auto-complete spans into the new result and fixes the weird behaviour of repetition when selecting autocomplete suggestions.