Skip to content

Instantly share code, notes, and snippets.

@kaushikgopal
Created June 25, 2016 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushikgopal/ea0d81206815734a9964eb984c6d5fb7 to your computer and use it in GitHub Desktop.
Save kaushikgopal/ea0d81206815734a9964eb984c6d5fb7 to your computer and use it in GitHub Desktop.
Blog post snippet
// the below code is inside a TextWatcher
// which implements the onTextChanged method
// I've simplified it to only highlight the parts we're
// interested in
private long lastChange = 0;
@Override
public void onTextChanged(final CharSequence chars,
int start, int before, int count) {
// The handler is spawned from the UI thread
new Handler().postDelayed(
// argument 1 for postDelated = message to be sent
new Runnable() {
@Override
public void run() {
if (noChangeInText_InTheLastFewSeconds()) {
searchAndPopulateListView(chars.toString()); // logic
}
}
},
// argument 2 for postDelated = delay before execution
300);
lastChange = System.currentTimeMillis();
}
private boolean noChangeInText_InTheLastFewSeconds() {
return System.currentTimeMillis() - lastChange >= 300
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment