Home > Software design >  How to get value from edittext in android without click button?
How to get value from edittext in android without click button?

Time:01-28

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.

CodePudding user response:

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
            }
        }
    })

CodePudding user response:

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);
  •  Tags:  
  • Related