Gopal Singh Sirvi
Gopal Singh Sirvi

Reputation: 4649

How to set limit of characters in EditText?

I am having an EditText and i set a maxLength to 2 but EditText is still taking more than 2 characters. Here is my code.

<EditText
            android:id="@+id/etAddClientcountry"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:maxLength="2"
            android:inputType="textCapCharacters"
            android:hint="Country (two letter ISO code)"
            android:textAppearance="@android:style/TextAppearance.Small" />

Upvotes: 1

Views: 7724

Answers (2)

Fahim
Fahim

Reputation: 12358

EditText edt =(EditText)findViewById(R.id.edt); 

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

Upvotes: 0

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11116

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

Upvotes: 14

Related Questions