MaxLength in EditText using Codes

Public on February 14, 2009
In android there is EditText and sometimes you may want to limit the character input. EditText in XML layout would give you android:maxLength to do this thing but in codes you might wonder why there isn't any setMaxLength function. The reason behind this is that when you want to restrict the EditText to accept certain value, you have to filter them and this would be invoke by setFilters and thus to make our EditText to have a fixed size we shall.
EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);


Reference:
androidblogger

Update History
   Jan 17, 2012 - Visual Update




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon