Set max length for EditText

omar wael picture omar wael · Sep 7, 2016 · Viewed 14.6k times · Source

I want to set a 1 character length in EditText but just in one part of condition. There is a way to set max length via XML but this will be applied for the whole condition. How to set max length programmatically?

Answer

Sangram Haladkar picture Sangram Haladkar · Sep 7, 2016
public void setEditTextMaxLength(EditText editText, int length) {
      InputFilter[] FilterArray = new InputFilter[1];
      FilterArray[0] = new InputFilter.LengthFilter(length);
      editText.setFilters(FilterArray);
  }