How to exclude special characters from android keypad for EditText - android

Hi I want to show only numbers and characters on the keypad for EditText in android, I did try to add the attribute android:inputType = text|number but it did not work.
Please help me with any other better suggestion. thanks in advance.

Use the filter for that. Here I am adding the code for filter.
EditText etName = (EditText)findViewById(R.id.etName);
InputFilter filter = 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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
etName.setFilters(new InputFilter[]{filter});

try to add the digits parameter to your editText:
android:digits="abcde.....012345789"

The solution with InputFilter provided here is not 100% correct as it will replace and throw out some valid characters from the input if they are right next to the invalid one.
For example we need to filter out all special characters and you enter text: olala[
The EditText field will pass the whole olala[ sentence to the filter and the return value will be "" meaning that we throw out valid olala as well.
Here is my solution:
InputFilter filter = (source, start, end, dest, dstart, dend)->{
for (int i = start; i < end; i++) {
char symbol = source.charAt(i);
if (!isValidCharacter(symbol)) {
StringBuilder buf = new StringBuilder();
for(int j = start; j < end; j++)
{
symbol = source.charAt(j);
if(isValidCharacter(symbol)) buf.append(symbol);
}
return buf.toString();
}
}
return null;
};
We need double loop here to avoid memory allocation of StringBuilder for every method call with valid characters.

If you want to add spaces you can give space after the last digit.
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 "

try using this method it worked for me:
public static InputFilter[] getFilter(String blockChars) {
return new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
for (int i = start; i < end; i++) {
if (source != null && blockChars.contains("" + source.charAt(i))) {
return source.subSequence(start, i);
}
}
return null;
}};
add this line for your edit text
edittext.setFilters(getFilter("#~"));

The expected solution is to restrict user from entering special characters from keyboard.
The below solution uses RegX but adds it to strings.xml file so that will be taken care at the time of creating multilingual xmls.
Strings.xml
<string name="alpha_numeric_regx">[a-zA-Z 0-9]+</string>
Source file
//Extracting forehand to avoid multiple calls to getString from Filter
String alphaNumericRegX = getString(R.string.alpha_numeric_regx);
mEditTextOtp.setFilters(new InputFilter[]{(source, start, end, dest, dStart, dEnd) -> {
for (int i = 0; i < source.length(); i++) {
if (source.equals("")) {
return source;
}
if (source.toString().matches(alphaNumericRegX)) {
return source;
}
return "";
}
return null;
}});
Hope this will solve the prob. for new guys. :)

Kotlin
et_search.keyListener = DigitsKeyListener.getInstance(getString(R.string.alphanumeric))
String.xml
<string name="alphanumeric">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</string>

//大兄弟,这么做就可以了。
InputFilter filter = 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 (isChineseChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
etName.setFilters(new InputFilter[]{filter});
//一条简单的规则。
private static boolean isChineseChar(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS;
}

Its Working
Restrict Special Symbol in Edittext
private EditText your_editText ;
private String blockCharacters = "(~*#^|$%&!";
private InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && blockCharacters.contains(("" + source))) {
return "";
}
return null;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
your_editText = (EditText) findViewById(R.id.your_editText);
your_editText .setFilters(new InputFilter[] { filter });
}
}

I couldn't edit a previous answer, so I did a Kotlin version of it (including whitespace):
private val userNameFilter =
InputFilter { source, start, end, dest, dStart, dEnd ->
for (i in start until end) {
if (!Character.isLetterOrDigit(source[i]) && !Character.isWhitespace(source[i])) {
return#InputFilter ""
}
}
null
}

Related

Android Alphanumeric only, programmatically (NOT XML)

I have a program in Android, with one AutoCompleteTextView named aCTVNumeroPoste.
This AutoCompleteTextView change the type of input depends of other option.
So Option 1 makes the AutoCompleteTextView only Text and Option 2 makes the AutoCompleteTextView only Numbers. And this seems to works well with the keyboard.
But THE PROBLEM is when the AutoCompleteTextView is Text, I only want Alphanumeric, and the keyboard allow me to introduce $%&/()=?¡"!-.,etc.
Heres the code:
if (accion.equals("MANTENIMIENTO")) {
aCTVNumeroPoste.setInputType(InputType.TYPE_CLASS_TEXT);
int maxLengthofEditText = 19;
aCTVNumeroPoste.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
}
else {
aCTVNumeroPoste.setInputType(InputType.TYPE_CLASS_NUMBER);
aCTVNumeroPoste.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
int maxLengthofEditText = 3;
aCTVNumeroPoste.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLengthofEditText)});
}
And i want to resolve this Programmatically, not in XML (with android:digits=...).
I tried to use this code, after InputType.TYPE_CLASS_TEXT:
aCTVNumeroPoste.setFilters(new InputFilter[]{
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if (src.equals("")) {
return src;
}
if (src.toString().matches("[a-zA-Z 0-9]+")) {
return src;
}
return "";
}
}
});
And this one too:
aCTVNumeroPoste.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "));
But nothing has worked. Any help is appreciated.
I answer my own question.
From this post How to restrict the EditText to accept only alphanumeric characters
This code works for me:
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();
}
}
And then add this after InputType.TYPE_CLASS_TEXT:
// Apply the filters to control the input (alphanumeric)
ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(aCTVNumeroPoste.getFilters()));
curInputFilters.add(0, new AlphaNumericInputFilter());
curInputFilters.add(1, new InputFilter.LengthFilter(maxLengthofEditText));
InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
aCTVNumeroPoste.setFilters(newInputFilters);

how to avoid characters like �‎ in an edittext?

I have an issue when user try to enter text copied that this text sometimes contains some special characters like �
and this make JSON string to be not formated, so please how can I avoid user enter such characters
please take into consideration that user can enter Arabic text and English text only
Try using InputFilters on the Edittext:
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = start; i < end; i++) {
if (isEnglishOrArabicChar(source.charAt(i))) {
stringBuilder.append(source.charAt(i));
}
}
return stringBuilder.toString();
}
};
etName.setFilters(new InputFilter[]{filter});
private static boolean isEnglishOrArabicChar(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return ub == Character.UnicodeBlock.ARABIC || ub==Character.UnicodeBlock.BASIC_LATIN;
}
Reference

android- restrict edittext to accept farsi characters only

I want to restrict my edittext to only accept farsi charecters . charecters like ض ص ث ق ف And so on .
I've tried to use xml digit but it did not work .
how can I do so ?
You can use InputFilter for the EditText. InputFilters can be attached to Editables to constrain the changes that can be made to them.
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 text = String.valueOf(source.charAt(i));
Pattern RTL_CHARACTERS = Pattern.compile("[\u0600-
\u06FF\u0750-\u077F\u0590-\u05FF\uFE70-\uFEFF]");
Matcher matcher = RTL_CHARACTERS.matcher(text);
if (matcher.find()) {
return ""; // it's Persian
}
}
return null;
}
};
Use this filter to detect Farsi/Persian characters and restrict them for edittext. Set this filter to your EditText using
editText.setFilters(new InputFilter[]{filter});
This will restrict Farsi/Persian characters
These code can help too:
InputFilter nameFilter = 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.isLetterOrDigit(source.charAt(i))) { // if digit!
String blockCharacterSet = getString(R.string.all_fa_words) + ' ';
if (blockCharacterSet.contains(String.valueOf(source.charAt(i)))) {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.materialWhite));
edtNameEdit.setHint(getString(R.string.name_lastname));
return null;
} else {
edtNameEdit.setHintTextColor(getResources().getColor(R.color.sPink));
edtNameEdit.setHint(getString(R.string.name_lastname) + ' ' + getString(R.string.must_fa));
return edtNameEdit.getText().toString();
}
} else {
return null;
}
}
return null;
}
};
edtNameEdit.setFilters(new InputFilter[]{nameFilter});
edtNameEdit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

EditText to restrict entering special characters: single quote, double quote and emojis

I have a problem while inserting special characters: single quote, double quote and emojis into the database from editText.
I want my editText to restrict those characters, and I was successful in restricting emojis, but I failed to restrict users entering single quote and double quote.
E.g. When I try to enter text Today's List from editText into the database it generates an exception.
I have used InputFilter in editText to filter emojis, and I want this filter to restrict single quote and double quotes too.
public static InputFilter getEditTextFilterEmoji() {
return new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
CharSequence sourceOriginal = source;
source = replaceEmoji(source);
end = source.toString().length();
if (end == 0)
return ""; //Return empty string if the input character is already removed
if (!sourceOriginal.toString().equals(source.toString())) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);
return sp;
} else {
return s;
}
} else {
return null; // keep original
}
}
private String replaceEmoji(CharSequence source) {
String notAllowedCharactersRegex = "[^a-zA-Z0-9##\\$%\\&\\-\\+\\(\\)\\*;:!\\?\\~`£\\{\\}\\[\\]=\\.,_/\\\\\\s'\\\"<>\\^\\|÷×]";
return source.toString()
.replaceAll(notAllowedCharactersRegex, "");
}
};
}
Can anyone help me with this ?
Restrict the EditText to use given digits only.
<EditText
........
android:inputType="textPersonName"
android:digits="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY zZ0123456789"
/>
As per my understanding all emoji contains /u so I used it like this
public static InputFilter[] getEmojiFilter(String blockChars) {
return new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
String source1= StringEscapeUtils.escapeJava(source.toString());
for (int i = start; i < end; i++) {
if (source != null && blockChars.contains("" + source1.charAt(i))) {
return source.subSequence(start, i);
}
}
return null;
}};
}
and Write below line for Edittext
editext.setFilters(getEmojiFilter("'\"\\//\\u"));
If you want alphabets only in edittext, Add this line in edittext tag
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Try this one....worked for me
private EditText InputText;
private String blockCharacterSet = "~#^|$%&*!"; //Special characters to block
private InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputText = (EditText) findViewById(R.id.InputText);
InputText.setFilters(new InputFilter[] { filter });
}
Add This in Strings.xml. It's allow u to enter alphanumeric and space also.
<string name="charset">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z</string>
This answer worked for me. I searched different questions and I got my answer.
setFilters(new InputFilter[]{new EmojiExcludeFilter()});
private class EmojiExcludeFilter implements InputFilter {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String specialChars = "/*!##$%^&*()\"{}_[]|\\?/<>,.:-'';§£¥...";
for (int i = start; i < end; i++) {
int type = Character.getType(source.charAt(i));
if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL || type == Character.MATH_SYMBOL || specialChars.contains("" + source)|| Character.isWhitespace(0)) {
return "";
}
}
return null;
}
}

How do I use InputFilter to limit characters in an EditText in Android?

I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the docs.
I found this on another forum. Works like a champ.
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 });
InputFilters are a little complicated in Android versions that display dictionary suggestions. You sometimes get a SpannableStringBuilder, sometimes a plain String in the source parameter.
The following InputFilter should work. Feel free to improve this code!
new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (source instanceof SpannableStringBuilder) {
SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source;
for (int i = end - 1; i >= start; i--) {
char currentChar = source.charAt(i);
if (!Character.isLetterOrDigit(currentChar) && !Character.isSpaceChar(currentChar)) {
sourceAsSpannableBuilder.delete(i, i+1);
}
}
return source;
} else {
StringBuilder filteredStringBuilder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
if (Character.isLetterOrDigit(currentChar) || Character.isSpaceChar(currentChar)) {
filteredStringBuilder.append(currentChar);
}
}
return filteredStringBuilder.toString();
}
}
}
much easier:
<EditText
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm" />
None of posted answers did work for me. I came with my own solution:
InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) // put your condition here
sb.append(c);
else
keepOriginal = false;
}
if (keepOriginal)
return null;
else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
return Character.isLetterOrDigit(c) || Character.isSpaceChar(c);
}
}
editText.setFilters(new InputFilter[] { filter });
Use this its work 100% your need and very simple.
<EditText
android:inputType="textFilter"
android:digits="#string/myAlphaNumeric" />
In strings.xml
<string name="myAlphaNumeric">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</string>
To avoid Special Characters in input type
public static InputFilter filter = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = "~#^|$%*!#/()-'\":;,?{}=!$^';,?×÷<>{}€£¥₩%~`¤♡♥_|《》¡¿°•○●□■◇◆♧♣▲▼▶◀↑↓←→☆★▪:-);-):-D:-(:'(:O 1234567890";
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
You can set filter to your edit text like below
edtText.setFilters(new InputFilter[] { filter });
I have done something like this to keep it simple:
edit_text.filters = arrayOf(object : InputFilter {
override fun filter(
source: CharSequence?,
start: Int,
end: Int,
dest: Spanned?,
dstart: Int,
dend: Int
): CharSequence? {
return source?.subSequence(start, end)
?.replace(Regex("[^A-Za-z0-9 ]"), "")
}
})
This way we are replacing all the unwanted characters in the new part of the source string with an empty string.
The edit_text variable is the EditText object we are referring to.
The code is written in kotlin.
First add into strings.xml:
<string name="vin_code_mask">0123456789abcdefghjklmnprstuvwxyz</string>
XML:
android:digits="#string/vin_code_mask"
Code in Kotlin:
edit_text.filters += InputFilter { source, start, end, _, _, _ ->
val mask = getString(R.string.vin_code_mask)
for (i in start until end) {
if (!mask.contains(source[i])) {
return#InputFilter ""
}
}
null
}
Strange, but it works weirdly on emulator's soft keyboard.
Warning! The following code will filter all letters and other symbols except digits for software keyboards. Only digital keyboard will appear on smartphones.
edit_text.keyListener = DigitsKeyListener.getInstance(context.getString(R.string.vin_code_mask))
I also usually set maxLength, filters, inputType.
In addition to the accepted answer, it is also possible to use e.g.: android:inputType="textCapCharacters" as an attribute of <EditText> in order to only accept upper case characters (and numbers).
It's Right, the best way to go about it to fix it in the XML Layout itself using:
<EditText
android:inputType="text"
android:digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" />
as rightly pointed by Florian Fröhlich, it works well for text views even.
<TextView
android:inputType="text"
android:digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" />
Just a word of caution, the characters mentioned in the android:digits will only be displayed, so just be careful not to miss any set of characters out :)
For some reason the android.text.LoginFilter class's constructor is package-scoped, so you can't directly extend it (even though it would be identical to this code). But you can extend LoginFilter.UsernameFilterGeneric! Then you just have this:
class ABCFilter extends LoginFilter.UsernameFilterGeneric {
public UsernameFilter() {
super(false); // false prevents not-allowed characters from being appended
}
#Override
public boolean isAllowed(char c) {
if ('A' <= c && c <= 'C')
return true;
if ('a' <= c && c <= 'c')
return true;
return false;
}
}
This isn't really documented, but it's part of the core lib, and the source is straightforward. I've been using it for a while now, so far no problems, though I admit I haven't tried doing anything complex involving spannables.
You can specify wanted characters in a regex and use it in InputFilter:
val regex = Regex("[a-zA-Z\\d ]")
editText.filters = arrayOf(InputFilter { source, _, _, _, _, _ ->
source.filter { regex.matches(it.toString()) }
})
Notice, I didn't use \w character class, because it includes underscore _
This simple solution worked for me when I needed to prevent the user from entering empty strings into an EditText. You can of course add more characters:
InputFilter textFilter = new InputFilter() {
#Override
public CharSequence filter(CharSequence c, int arg1, int arg2,
Spanned arg3, int arg4, int arg5) {
StringBuilder sbText = new StringBuilder(c);
String text = sbText.toString();
if (text.contains(" ")) {
return "";
}
return c;
}
};
private void setTextFilter(EditText editText) {
editText.setFilters(new InputFilter[]{textFilter});
}
This is an old thread, but the purposed solutions all have issues (depending on device / Android version / Keyboard).
DIFFERENT APPROACH
So eventually I went with a different approach, instead of using the InputFilter problematic implementation, I am using TextWatcher and the TextChangedListener of the EditText.
FULL CODE (EXAMPLE)
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable editable) {
super.afterTextChanged(editable);
String originalText = editable.toString();
int originalTextLength = originalText.length();
int currentSelection = editText.getSelectionStart();
// Create the filtered text
StringBuilder sb = new StringBuilder();
boolean hasChanged = false;
for (int i = 0; i < originalTextLength; i++) {
char currentChar = originalText.charAt(i);
if (isAllowed(currentChar)) {
sb.append(currentChar);
} else {
hasChanged = true;
if (currentSelection >= i) {
currentSelection--;
}
}
}
// If we filtered something, update the text and the cursor location
if (hasChanged) {
String newText = sb.toString();
editText.setText(newText);
editText.setSelection(currentSelection);
}
}
private boolean isAllowed(char c) {
// TODO: Add the filter logic here
return Character.isLetter(c) || Character.isSpaceChar(c);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do Nothing
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do Nothing
}
});
The reason InputFilter is not a good solution in Android is since it depends on the keyboard implementation. The Keyboard input is being filtered before the input is passed to the EditText. But, because some keyboards have different implementations for the InputFilter.filter() invocation, this is problematic.
On the other hand TextWatcher does not care about the keyboard implementation, it allows us to create a simple solution and be sure it will work on all devices.
If you subclass InputFilter you can create your own InputFilter that would filter out any non-alpha-numeric characters.
The InputFilter Interface has one method, filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend), and it provides you with all the information you need to know about which characters were entered into the EditText it is assigned to.
Once you have created your own InputFilter, you can assign it to the EditText by calling setFilters(...).
http://developer.android.com/reference/android/text/InputFilter.html#filter(java.lang.CharSequence, int, int, android.text.Spanned, int, int)
Ignoring the span stuff that other people have dealt with, to properly handle dictionary suggestions I found the following code works.
The source grows as the suggestion grows so we have to look at how many characters it's actually expecting us to replace before we return anything.
If we don't have any invalid characters, return null so that the default replacement occurs.
Otherwise we need to extract out the valid characters from the substring that's ACTUALLY going to be placed into the EditText.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
boolean includesInvalidCharacter = false;
StringBuilder stringBuilder = new StringBuilder();
int destLength = dend - dstart + 1;
int adjustStart = source.length() - destLength;
for(int i=start ; i<end ; i++) {
char sourceChar = source.charAt(i);
if(Character.isLetterOrDigit(sourceChar)) {
if(i >= adjustStart)
stringBuilder.append(sourceChar);
} else
includesInvalidCharacter = true;
}
return includesInvalidCharacter ? stringBuilder : null;
}
};
to prevent words in edittext.
create a class that u could use anytime.
public class Wordfilter implements InputFilter
{
#Override
public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend) {
// TODO Auto-generated method stub
boolean append = false;
String text = source.toString().substring(start, end);
StringBuilder str = new StringBuilder(dest.toString());
if(dstart == str.length())
{
append = true;
str.append(text);
}
else
str.replace(dstart, dend, text);
if(str.toString().contains("aaaaaaaaaaaa/*the word here*/aaaaaaaa"))
{
if(append==true)
return "";
else
return dest.subSequence(dstart, dend);
}
return null;
}
}
It is possible to use setOnKeyListener. In this method, we can customize the input edittext !
This is how I created filter for the Name field in Edit Text.(First letter is CAPS, and allow only single space after every word.
public void setNameFilter() {
InputFilter filter = new InputFilter() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (dend == 0) {
if (Character.isSpaceChar(source.charAt(i)) ||
!Character.isAlphabetic(source.charAt(i))) {
return Constants.Delimiter.BLANK;
} else {
return String.valueOf(source.charAt(i)).toUpperCase();
}
} else if (Character.isSpaceChar(source.charAt(i)) &&
String.valueOf(dest).endsWith(Constants.Delimiter.ONE_SPACE)) {
return Constants.Delimiter.BLANK;
} else if ((!Character.isSpaceChar(source.charAt(i)) &&
!Character.isAlphabetic(source.charAt(i)))) {
return Constants.Delimiter.BLANK;
}
}
return null;
}
};
editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(Constants.Length.NAME_LENGTH)});
}
I have the same answer in Kotlin:
/**
* Returns the filter of the editText'es CharSequence value when [filterType] is:
* 1 -> letters; 2 -> letters and digits; 3 -> digits;
* 4 -> digits and dots
*/
class InputFilterAlphanumeric(private val filterType: Int): InputFilter {
override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence {
(source as? SpannableStringBuilder)?.let {sourceAsSpannableBuilder ->
for (i in (end - 1) downTo start) {
val currentChar = source[i]
when(filterType) {
1 -> {
if (!currentChar.isLetter() && !currentChar.isWhitespace()) {
sourceAsSpannableBuilder.delete(i, i + 1)
}
}
2 -> {
if (!currentChar.isLetterOrDigit() && !currentChar.isWhitespace()) {
sourceAsSpannableBuilder.delete(i, i + 1)
}
}
3 -> {
if (!currentChar.isDigit()) {
sourceAsSpannableBuilder.delete(i, i + 1)
}
}
4 -> {
if (!currentChar.isDigit() || !currentChar.toString().contains(".")) {
sourceAsSpannableBuilder.delete(i, i + 1)
}
}
}
}
return source
} ?: run {
val filteredStringBuilder = StringBuilder()
for (i in start until end) {
val currentChar = source?.get(i)
when(filterType) {
1 -> {
if (currentChar?.isLetter()!! || currentChar.isWhitespace()) {
filteredStringBuilder.append(currentChar)
}
}
2 -> {
if (currentChar?.isLetterOrDigit()!! || currentChar.isWhitespace()) {
filteredStringBuilder.append(currentChar)
}
}
3 -> {
if (currentChar?.isDigit()!!) {
filteredStringBuilder.append(currentChar)
}
}
4 -> {
if (currentChar?.isDigit()!! || currentChar.toString().contains(".")) {
filteredStringBuilder.append(currentChar)
}
}
}
}
return filteredStringBuilder
}
}
}
and get the class with an Extension function:
fun EditText.filterByDataType(filterType: Int) {
this.filters = arrayOf<InputFilter>(InputFilterAlphanumeric(filterType))
}

Categories

Resources