How to Use Inputfilter to Limit Characters in an Edittext in Android

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

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 });

What's the best way to limit text length of EditText in Android

Documentation

Example

android:maxLength="10"

Restrict Characters in Android EditText

Try this one

InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {

if(source.length() > 10) return "";
else{
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
}

return null;
}
};

Then set it to you editext

myEditxt.setFilters(new InputFilter[] { filter });

How to set limit of characters in EditText?

EditText editText = new EditText(this);  
int maxLength = 3;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});

How to filter the input of EditText?

Add InputFilter to your EditText & provide a Toast for user . This code snippet will help you.

 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))) { // Accept only letter & digits ; otherwise just return
Toast.makeText(context,"Invalid Input",Toast.LENGTH_SHORT).show();
return "";
}
}
return null;
}

};

editText.setFilters(new InputFilter[] { filter });

How to restrict some character in editText

You can restrict the characters that the user can enter in the EditText by setting a TextWatcher on the widget and inserting the logic below:

// we are interested in this callback
@Override
public void afterTextChanged(Editable s) {
String result = s.toString().replaceAll("\\{", "");
if (!s.toString().equals(result)) {
edit.setText(result); // "edit" being the EditText on which the TextWatcher was set
edit.setSelection(result.length()); // to set the cursor at the end of the current text
}
}

The \\ is required(and for other characters) because the { character has a special meaning in a pattern.

Android:: Set max-length of EditText programmatically with other InputFilter

Just try this way

InputFilter

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 (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
{
return "";
}
}

return null;
}
};

How to apply

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edt =(EditText)findViewById(R.id.edt) ;

edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});


}


Related Topics



Leave a reply



Submit