Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active February 6, 2021 08:47
Show Gist options
  • Save gotev/46e4eaf2bfb522512d4c0e2c73e7d23b to your computer and use it in GitHub Desktop.
Save gotev/46e4eaf2bfb522512d4c0e2c73e7d23b to your computer and use it in GitHub Desktop.
Android Throttled Search
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.util.Timer;
import java.util.TimerTask;
/**
* Throttles a search action by an amount of milliseconds.
*
* @author Aleksandar Gotev
*/
public class ThrottledSearch {
public interface Delegate {
void onThrottledSearch(String searchTerm);
}
private static final int DEFAULT_DELAY = 400;
private int mDelayMilliseconds;
private final Delegate mDelegate;
private String mSearchTerm;
private Timer mTimer;
private AppCompatActivity mActivity;
public ThrottledSearch(AppCompatActivity activity, Delegate delegate, int milliseconds) {
mActivity = activity;
mDelayMilliseconds = milliseconds;
mDelegate = delegate;
mSearchTerm = "";
}
public ThrottledSearch(AppCompatActivity activity, Delegate delegate) {
this(activity, delegate, DEFAULT_DELAY);
}
public void onTextChanged(CharSequence charSequence) {
if (mTimer != null)
mTimer.cancel();
mTimer = new Timer();
mSearchTerm = charSequence.toString();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mDelegate.onThrottledSearch(mSearchTerm);
}
});
}
}, mDelayMilliseconds);
}
public void attachTo(final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
ThrottledSearch.this.onTextChanged(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
public void attachTo(final SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
ThrottledSearch.this.onTextChanged(newText);
return true;
}
});
}
}
@gotev
Copy link
Author

gotev commented Dec 13, 2016

Usage:

new ThrottledSearch(this, new ThrottledSearch.Delegate() {
    @Override
    public void onThrottledSearch(String searchTerm) {
        // do something with the search term
    }
}).attachTo(EditText or SearchView);

alternatively, you can create a new object:

ThrottledSearch search = new ThrottledSearch(activity, new ThrottledSearch.Delegate() {
    @Override
    public void onThrottledSearch(String searchTerm) {
        // do something
    }
});

and then call search.onTextChanged("something"); in your own edit text listener.

You can also set the throttle amount in milliseconds in the constructor (by default it's 400ms):

new ThrottledSearch(activity, delegate, milliseconds);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment