Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

I'm using InputFilter like this to allow only alpha and numbers

private InputFilter[] inputFilters = new InputFilter[] { 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;     } } }; 

But problem is "android:maxLength" value in xml file is not working with this InputFilter

I think I need code in InputFilter to set max-length of EditText

Anyone has good idea for this?

Thanks

like image 222
pokoso Avatar asked Jul 15 '14 04:07

pokoso


People also ask

Is there a way to define a min and max value for Edittext in Android?

setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")}); This will allow user to enter values from 1 to 12 only. EDIT : Set your edittext with android:inputType="number" .


1 Answers

A simple one-liner would be:

myEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(10) });  //replace 10 with required length. 
like image 72
Divins Mathew Avatar answered Sep 17 '22 15:09

Divins Mathew