Saturday, February 14, 2009

MaxLength in EditText using Codes

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
Post a Comment