-2

Im making a program where a user has to search through some of my data.

I've created an edit view for the user to put their text in to, but I want to run a method once the user has confirmed their input.

How do I do this?

Thank you.

Joshua Best
  • 246
  • 1
  • 14
  • Do you have a confirm button? If yes, then use its `onClickListener`... – deHaar Jul 02 '18 at 10:13
  • set OnClickListener on confirm-button and editText.getText().toString() once clickListener onClick is called – JacksOnF1re Jul 02 '18 at 10:13
  • Please show us some code first – Adriaan Koster Jul 02 '18 at 10:14
  • @deHaar I want to use the confirm button on the users keyboard. – Joshua Best Jul 02 '18 at 10:14
  • @AdriaanKoster There isn't any code. This is the start of my activity. – Joshua Best Jul 02 '18 at 10:15
  • Then read the documentation of EditText first. – JacksOnF1re Jul 02 '18 at 10:16
  • 3
    Unfortunately your question boils down to "somebody please please help me with this". But we do not regard such requests as *questions* in the scope of this site. Please read [this](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) carefully to understand why that is. Then consider to either delete this question and putting up a new, more precise question within the scope of this community. Alternatively, you could [edit], rework and improve this question. Thanks! – GhostCat Jul 02 '18 at 10:17
  • duplicate of https://stackoverflow.com/questions/5677563/listener-for-done-button-on-edittext – Gaurav Chauhan Jul 02 '18 at 10:30

2 Answers2

1

If you are trying to add a search feature , why not use a search view instead.

Anjani Mittal
  • 507
  • 1
  • 7
  • 19
0

there are many options to do this, i am writing 3 options below.

1 - If you have confirm button then use its onclicklistener for you method call.

2 - If you don't have confirm button then add focusChangeListener in edittext and call you method when edittext focus is lost.

3 - You can use a timerTask like when user stopped typing for 2 second (we assume that user input is done) you can call your method

Example:

    edt_search.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) {
            // user is typing: reset already started timer (if existing)
            if (timer != null) {
                timer.cancel();
            }
        }
        @Override
        public void afterTextChanged(final Editable s) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {

                    // Search Logic
                       if (!s.toString().trim().isEmpty())
                       // Your Function call
                }
            }, 500); // 350ms delay before the timer executes the "run“ method from TimerTask
        }
}
Arpit bandil
  • 204
  • 2
  • 9