Allow only 1 character in EditText and always overwrite when user enters it

I need to do an EditText that will only accept one character and only a character (letter / alpha). And if the user enters another char, he must replace the existing one (for example, a method of rewriting text input with 1 valid character).

  • I know how to set the maximum length of text in properties. But if I set it to 1, then no other char can be inserted before the user removes the existing one. But I want to replace the existing char with the newly entered one automatically without manual deletion. How to do it?

  • I know how to set a property to allow only numbers in an EditText , but I cannot figure out how to allow only characters. So the second question is how to allow only characters in an EditText ?

I am currently using EditText with maximum text size = 2 with the following code:

 final EditText editLetter = (EditText)findViewById(R.id.editHouseLetter); editLetter.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { if (s!=null && s.length()>1){ editLetter.setText(s.subSequence(1, s.length())); editLetter.setSelection(1); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } }); 

The fact is that when the user enters the second char, the first must be deleted. If the text size is 2, the user can enter another char after it has already been entered.

I really don't understand how this works, but it does :). And in addition, I had to move the cursor over EditText to the last position, because it always goes to the beginning, which makes it impossible to enter anything. It didn’t work out why this is either.

The main point of this solution is that it has a size of 2 tars EditText , but I want it 1 char. And it allows you to enter anything except letters (characters / alpha), and I want nothing but characters.

Using the recommendations of sgarman and Character.isLetter() , the afterTextChanged method looks like this:

 public void afterTextChanged(Editable s) { int iLen=s.length(); if (iLen>0 && !Character.isLetter((s.charAt(iLen-1)))){ s.delete(iLen-1, iLen); return; } if (iLen>1){ s.delete(0, 1); } } 

I found that using Selection.setSelection is not required in this case. Now he has a filter that allows you to enter only letters. This is almost the answer I want. Only one thing remains: to do the same with the 1-character size of the EditText ?

+6
android android-edittext textwatcher
source share
3 answers

Try:

 s.delete(0, 1); Selection.setSelection(s, s.length()); 
+3
source share

You can add this single line to your XML layout file, and android will do everything for you.

 android:maxLength="1" 

of course you should add it as a property in editText elemet, for example

 <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" android:maxLength="1" > 

I ran into the same problem and found that this is a simpler solution, I know this is an old question, but I decided to put my answer in order to make it easier for those who open this link.

And I must not forget to thank the owner of this blog , which of which I quoted the answer!

+9
source share

I wanted to do the same, and I used your code in afterTextChanged (Editable s) and also set one filter in editText like this.

 editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(2)}); 

He worked the way we wanted.

+2
source share

All Articles