78

I am filtering my list using an EditText control. I want to filter the list 0.5 seconds after the user has finished typing in EditText. I used the afterTextChanged event of TextWatcher for this purpose. But this event rises for each character changes in EditText.

What should I do?

18 Answers 18

173

Use:

editText.addTextChangedListener(
    new TextWatcher() {
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        private Timer timer = new Timer();
        private final long DELAY = 1000; // Milliseconds

        @Override
        public void afterTextChanged(final Editable s) {
            timer.cancel();
            timer = new Timer();
            timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: Do what you need here (refresh list).
                        // You will probably need to use
                        // runOnUiThread(Runnable action) for some
                        // specific actions (e.g., manipulating views).
                    }
                },
                DELAY
            );
        }
    }
);

The trick is in canceling and rescheduling Timer each time, when text in EditText gets changed.

For how long to set the delay, see this post.

7
  • 3
    Check this once, stackoverflow.com/questions/10217051/…
    – Saurabh
    Oct 23, 2013 at 7:30
  • Where is delayInMillis defined? Can it be passed in somehow? May 22, 2014 at 19:07
  • Sure, delayInMillis is any long variable representing the delay (0.5s in this case). You can also use 500 instead of that. See my updated answer.
    – Berťák
    May 23, 2014 at 8:29
  • 1
    For those who want to runonUiThread ... ((Activity)getContext()).runOnUiThread() or getActivity().runOnUiThread() Mar 6, 2017 at 6:23
  • If the TimerTask's code is large, would it be better not to create it each time (because schedule will be called at each keystroke), but reuse one object? I have also tried not to create the time again, but it seemed that when a timer is once cancelled, it cannot be reused for a new schedule. Jan 19, 2018 at 20:50
56

Better use Handler with the postDelayed() method. In the Android's implementation, Timer will create a new thread each time to run the task. Handler, however, has its own Looper that can be attached to whatever thread we wish, so we won't pay an extra cost to create a thread.

Example

 Handler handler = new Handler(Looper.getMainLooper() /*UI thread*/);
 Runnable workRunnable;
 @Override public void afterTextChanged(Editable s) {
    handler.removeCallbacks(workRunnable);
    workRunnable = () -> doSmth(s.toString());
    handler.postDelayed(workRunnable, 500 /*delay*/);
 }

 private final void doSmth(String str) {
    //
 }
4
  • 1
    Why do you need to remove the callbacks?
    – TheLearner
    Mar 2, 2018 at 2:37
  • @TheLearner So it won't get called multiple times. If you don't cancel the previous one, it's still getting executed. Which brings us back to the whole point of this question, to debounce it.
    – Denny
    Aug 21, 2018 at 18:08
  • I know this is an old answer, but what do you think happens if you've scheduled this Runnable to be called after a while and the context is removed for instance because the user moves away from this screen?
    – Darwind
    Sep 7, 2018 at 8:25
  • handler.removeCallbacks(workRunnable); should be additionally called in onPause of Fragment/Activity to prevent running when the UI is not showing.
    – MatPag
    Jan 15, 2021 at 9:12
17

You can use RxBindings; it's the best solution. See the guide to RxJava operator debounce. I'm sure that will do great in your case.

RxTextView.textChanges(editTextVariableName)
            .debounce(500, TimeUnit.MILLISECONDS)
            .subscribe(new Action1<String>() {
                @Override
                public void call(String value) {
                    // Do some work with the updated text
                }
            });
1
  • You'll probably also need .observeOn(AndroidSchedulers.mainThread()) for this to work
    – Isen Ng
    Oct 22, 2021 at 8:38
17

With Kotlin extension functions and coroutines:

fun AppCompatEditText.afterTextChangedDebounce(delayMillis: Long, input: (String) -> Unit) {
    var lastInput = ""
    var debounceJob: Job? = null
    val uiScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
    this.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            if (editable != null) {
                val newtInput = editable.toString()
                debounceJob?.cancel()
                if (lastInput != newtInput) {
                    lastInput = newtInput
                    debounceJob = uiScope.launch {
                        delay(delayMillis)
                        if (lastInput == newtInput) {
                            input(newtInput)
                        }
                    }
                }
            }
        }

        override fun beforeTextChanged(cs: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(cs: CharSequence?, start: Int, before: Int, count: Int) {}
})}
1
  • Could someone explain why the delay is added inside of UI scope? Does it not suspend the main thread? Jan 5, 2022 at 4:48
13

None of the above solutions worked for me.

I needed a way for TextWatcher to not fire on every character I input inside my search view and show some progress, meaning I need to access the UI thread.

private final TextWatcher textWatcherSearchListener = new TextWatcher() {
    final android.os.Handler handler = new android.os.Handler();
    Runnable runnable;

    public void onTextChanged(final CharSequence s, int start, final int before, int count) {
        handler.removeCallbacks(runnable);
    }

    @Override
    public void afterTextChanged(final Editable s) {
        // Show some progress, because you can access UI here
        runnable = new Runnable() {
            @Override
            public void run() {
                // Do some work with s.toString()
            }
        };
        handler.postDelayed(runnable, 500);
    }

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

Removing Handler on every onTextChanged (which is called when the user inputs a new character). afterTextChanged is called after the text has been changed inside the input field where we can start a new Runnable, but it will cancel it if the user types more characters (for more information, when these callback are called, see this). If the user doesn't input any more characters, the interval will pass in postDelayed and it will call work you should do with that text.

This code will run only once per interval, not for every key user inputs.

1
  • Can you explain, why we need to remove the callback and how the Handler is working in this case? Is it in the main thread? Trying to understand how all of this works.
    – TheLearner
    Mar 2, 2018 at 3:08
7

In the Kotlin language, you can do it like this:

tv_search.addTextChangedListener(mTextWatcher)

private val mTextWatcher: TextWatcher = object : TextWatcher {
    private var timer = Timer()
    private val DELAY: Long = 1000L

    override fun afterTextChanged(s: Editable?) {
        timer.cancel()
        timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {

                // Do your stuff here
            }
        }, DELAY)
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }

}
1
  • 1
    How efficient this is considering that every Timer instantiation also spawns a single background thread?
    – Yamin
    Apr 19, 2022 at 23:10
5

Another way to observe text change event is using Coroutines Channels.

lifecycleScope.launchWhenCreated {
            editText.afterTextChanged {
                // do something
            }
        }

Create an extension function to collect data from a flow

suspend fun EditText.afterTextChanged(afterTextChanged: suspend (String) -> Unit) {
    val watcher = Watcher()
    this.addTextChangedListener(watcher)

    watcher.asFlow()
        .debounce(500)
        .collect { afterTextChanged(it) }
}

Create a Watcher class to offer text after change

class Watcher : TextWatcher {

    private val channel = ConflatedBroadcastChannel<String>()

    override fun afterTextChanged(editable: Editable?) {
        channel.offer(editable.toString())
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

    fun asFlow(): Flow<String> {
        return channel.asFlow()
    }
}
4

How do you determine that they have finished writing? That the edittext loses focus? Then there is setOnFocusChangedListener.

Responding to latest edit in question: If you want to wait a specific time after the latest key stroke, then you have to start up a thread at the first keypress (use TextWatcher). Constantly register the time of the latest key stroke. Let the thread sleep to the the time of the latest keystroke + 0.5 seconds. If the timestamp of the latest keystroke has not been updated, do whatever you had intended.

1
  • 0.5 second after user has finished typing. How can I do that?
    – Bob
    Aug 27, 2012 at 12:50
3

You can also use the TextWatcher interface and create your custom class that implements it to reuse your CustomTextWatcher many times and also you can pass views or whatever you might need to its constructor:

public abstract class CustomTextWatcher implements TextWatcher { // Notice abstract class so we leave abstract method textWasChanged() for implementing class to define it

    private final TextView myTextView; // Remember EditText is a TextView, so this works for EditText also


    public AddressTextWatcher(TextView tView) { // Notice I'm passing a view at the constructor, but you can pass other variables or whatever you need
        myTextView = tView;
    }

    private Timer timer = new Timer();
    private final int DELAY = 500; // Milliseconds of delay for timer

    @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(final Editable s) {
        timer.cancel();
        timer = new Timer();

        timer.schedule(

            new TimerTask() {
                @Override
                public void run() {
                    textWasChanged();
                }
            },
            DELAY

        );
    }

    public abstract void textWasChanged(); // Notice the abstract method to leave the
                                           // implementation to the implementing class

}

Now in your activity you can use it like this:

// Notice I'm passing in the constructor of CustomTextWatcher
// myEditText I needed to use
myEditText.addTextChangedListener(new CustomTextWatcher(myEditText) {
    @Override
    public void textWasChanged() {
        //doSomething(); This is method inside your activity
    }
});
1
  • do I need to call runOnUIThread inside textWasChanged method for any UI changes/ update? Jan 21, 2017 at 21:33
2

You can use a timer. After typing the text it will wait for 600 ms. Put the code inside afterTextChanged() by using a delay of 600 ms.

@Override
    public void afterTextChanged(Editable arg0) {
        // The user typed: start the timer
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // Do your actual work here
               editText.setText(et.getText().toString());
            }
        }, 600); // 600 ms delay before the timer executes the „run“ method from TimerTask
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Nothing to do here
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // The user is typing: reset already started timer (if existing)
        if (timer != null) {
            timer.cancel();
        }
    }
};
0

Try this

class DelayTextWatcher(val ms: Long = 500, val textChanged: (String) -> Unit) : TextWatcher {

private var timer: CountDownTimer? = null
override fun afterTextChanged(p0: Editable) {
    timer?.cancel()
    timer = object : CountDownTimer(ms, ms) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            textChanged(p0.toString())
        }
    }.start()
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

fun dispose() {
    timer?.cancel()
}

}

0

That is the event while and after finish of typing ... add a textWatcher and in the onTextChanged method put:

if (charSequence.length() > 0){ 
    // Your code 
}
0

If you want to skip textWatcher for the first time only, then add the following code:

This will allow textWatcher make any change from the second time.

Boolean firstchange = false;

profileEmailEditText.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 (firstchange) {
            emailAlertText.setVisibility(View.VISIBLE);
        }
        else {
            firstchange = true;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
0

If you write in Kotlin, you can do it like this.

This approach uses coroutines instead Thread (if you will do it via Timer()). Moreover, you can control the lifecycle of debounceJob with launchWhenCreated, etc.

private val onNumberListener = object : TextWatcher {
    private var debounceJob: Job? = null
    private val DELAY: Long = 1000L

    override fun afterTextChanged(s: Editable?) {
        debounceJob?.cancel()
        debounceJob = [email protected]
            .launch(Dispatchers.Main) {
                delay(DELAY)
                viewModel.onNumberChange(s?.toString() ?: "")
            }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }
}
0

the best way is to move the cursor

setSelection(it.toString().length)

with this form don't use tree or coroutine to sleep N tine

0
0

if you use viewmodel to get api response you can use following code:

searchProductsViewModel.viewModelScope.launch {
                    delay(500)
                    searchProductsViewModel.products()
                }
-1

Using a timer for your case is not the best solution because of creating a new object every time. According to the Timer documentation, it's better to use ScheduledThreadPoolExecutor -

"Timers schedule one-shot or recurring tasks for execution. Prefer ScheduledThreadPoolExecutor for new code."

Here is a better approach

Runnable runnabledelayedTask = new Runnable(){
    @Override
    public void run(){
        //TODO Perform any operation here
    }
};

editText.addTextChangedListener(
    new TextWatcher() {
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        private final long DELAY = 500; // Milliseconds

        @Override
        public void afterTextChanged(final Editable s) {
            ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(1);
            ScheduledFuture sf = scheduledPool.schedule(callabledelayedTask, DELAY, TimeUnit.MILLISECONDS);
            // You can cancel ScheduledFuture when needed
        }
    }
);
-2

You can use EditorActionListener for that purpose.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            //Do something here
            return true;
        }
        return false;
    }
});
1
  • 1
    This is used when the user presses "Enter" or "Next" button.
    – Bob
    Aug 27, 2012 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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