[Solved] Edit text first time to input a letter validation


You, my friend, need a

EditText editText = (EditText)findViewById(R.id.edittext);
editText .addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            ; //Do nothing
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            ; //Do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //TODO put your code here.
        } 

    });

Now, just check to see if that “s” Char Sequence contains your letter and you’ll be on your way.


Edit:
Once you have that CharSequence, just check to see that the first char is a letter.

To get the first char, do

s.charAt(0);  //Note: if there is nothing here, it could throw a null pointer.

The Character class has a static function that you can use to see if it is a letter (or a number, capital, lowercase, digit, digit or letter, etc) called

Character.isLetter(char); 

so you could do something like

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Check to see that there is at least 1 char to look at.
            //Then check to see if it is a letter.
            //(Note: the && is AND, so both things have to be true)
            if (s.length() > 0 && Character.isLetter(s.charAt(0))) {
                ; //Display your button
            } else { 
                ; //Display your error
            }
        }

Does that help?


You can create a new class called MyTextWatcher (either in it’s own file where you can import it anywhere or as a class within a class (if all of the edit texts are in the same area)), and then inside there do the regular stuff you are now expert at:

public class MyTextWatcher implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {  
    }
}

I personally prefer doing it inside the class that contains the edittext, but it can go in a separate file (that you import) and then it’s more reusable. And if for any reason, you need to access things with your TextWatcher, you can pass them in as a constructor. The advantage of doing things inside the class is that you don’t have to pass things into a constructor. It’s just less things for you to do. But here is an example of that constructor…

public class MyTextWatcher implements TextWatcher {
    private Thing1 mThing1;
    private Thing2 mThing2;
    public MyTextWatcher(Thing1 t1, Thing2 t2) {
        mThing1 = t1;
        mThing2 = t2;
    }
    ...
}

Note: Just be careful not to interlink things too much when doing that. It can get hairy later on. 🙂

3

solved Edit text first time to input a letter validation