1

I want the edittext value without clicking any button.I want to enter something in edittext and after entering the value want to get the value and start a method.

  • Does this answer your question? [How to use the TextWatcher class in Android?](https://stackoverflow.com/questions/8543449/how-to-use-the-textwatcher-class-in-android) – javdromero Jan 27 '22 at 17:04

2 Answers2

2

You can use TextWatcher like this.

TextWatcher textWatcher = 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) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                Toast.makeText(AccountSettingsActivity.this, "New text is: " + s.toString(), Toast.LENGTH_SHORT).show();
            }
        };

And you can assing the TextWatcer in your EditText like this

editText.addTextChangedListener(textWatcher);
GSepetadelis
  • 272
  • 9
  • 24
1

you can use some listener for Edittext in android an watch all changes inside an edittext every time user type a new character in Edittext this listener invoked

here is my sample code in Kotlin:

 search_EditText.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable) {
           // Do something
            }
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
          // Do something
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            if (s.length != 0) {
                // Do something
            } else {
                  // Do something
            }
        }
    })
soheil ghanbari
  • 388
  • 1
  • 7