-3

I'm creating an application that checks if the user inputs a letter or a number then a button will pop-out. And if the user didn't enter a single letter a validation will show.

Here's an example of what I'm trying to debug[main activity. PS. not my real code]:

private EditText edittext;

    Button checkBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addKeyListener();
    }

    public void addKeyListener() {
        edittext = (EditText) findViewById(R.id.editText);
        checkBtn = (Button) findViewById(R.id.button1);

        checkBtn.setVisibility(View.INVISIBLE);


        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {


                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    checkBtn.setVisibility(View.VISIBLE);

                    return true;

                else if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_9)) {

                // display a floating message
                Toast.makeText(MyAndroidAppActivity.this,
                        "Number 9 is pressed!", Toast.LENGTH_LONG).show();
                return true;
            }

            return false;
}
        });

    }
}

Question: Is there a way to make the button show automatically when a user types a letter without clicking the enter key. And if the user didn't input a letter a validation will pop out saying invalid and the button will not display.

I tried looking for the right answer but still I couldn't find it.

1
  • simply add a textWatcher as mentioned by Jacob Holloway Apr 14, 2015 at 4:20

2 Answers 2

2

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
  • sorry but can you help me out. im just new in android coding. thanks Apr 14, 2015 at 4:12
  • just one last question is their a way to construct a code that can handle 6 edit text and call it in a private class declaration.thanks Apr 14, 2015 at 4:29
  • Added one more section to the bottom of your question. Hopefully it is clear enough. Apr 14, 2015 at 5:14
0

Try this I am not sure this is the exact solution you are expecting.

        button = (Button) findViewById(R.id.button1);
    button.setVisibility(View.GONE);

    editText.addTextChangedListener(new 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) {

            if(editText.getText().length() > 0 && !s.toString().equals("\n")){
                button.setVisibility(View.VISIBLE);
            }
            else{
                button.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, "Invalid", Toast.LENGTH_SHORT).show();
            }

       }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }); 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.