Permit only 1 character in EditText and always overwrite when user input it

Stan picture Stan · Apr 5, 2011 · Viewed 16k times · Source

I need to make an EditText which would accept only one character and only character (letter/alpha). And if user enters other char, it should replace existing one (like overwrite method of text input with 1 allowed symbol).

  1. I know how to set the max length of text in properties. But if I set it to 1, then no other char could be inserted before user deletes an existing one. But I want to replace existing char with that new entered one automatically without manual deleting. How to do that?

  2. I know how to set property to allow only digits in an EditText, but I can't figure out how to allow only chars. So the second question is how to allow only characters in EditText?

Currently I'm using an EditText with max 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 point is, when a user enters a second char, the first one should be deleted. Making text size to 2 allows user to enter another char after one has been already entered.

I don't really understand how it works but it does :). And additionally I had to point cursor in EditText to the last position because it always goes to beginning which makes unable to enter anything at all. Didn't get why is that either.

Main point of this solution is that it has a 2-chars sized EditText, but I want it 1-char sized. And it allows to enter anything besides the letters(chars/alpha) and I want nothing but chars.

Using the advice provided by sgarman and Character.isLetter() function, afterTextChanged method looks this way now:

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've discovered that using Selection.setSelection is not required in this case. Now it has a filter allowing input of letters only. It's almost the answer that I want. The only one thing left is how to do the same with 1-symbol sized EditText?

Answer

abdullah.cu picture abdullah.cu · Jul 7, 2013

You can add this single line to your layout xml file, and android will do the stuff for you

android:maxLength="1"

for sure you should add it as a property to the 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 faced the same problem and found that this easier solution, I know it's an old question, but I decided to put my answer to make it easier for those who open this link.

And I shouldn't forget to thank the owner of this blog, which from I quoted the answer!