Searchview Auto Submit After Couple Of Letters Or Seconds
I have a android.support.v7.widget.SearchView that I use like this: if (query == null) { // Associate searchable configuration with the SearchView SearchManager searchManage
Solution 1:
Use a editText instead of the searchView and add a textWatcher similar to this:
searchView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Insert your code here for submitting
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Solution 2:
Use this code, I just had the same problem and figured out how to implement it.
The code is commented and I believe it is self explanatory, otherwise feel free to ask anything!
@OverridepublicvoidonCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem searchBackupsButton = menu.findItem(R.id.action_search);
searchView = (SearchView) searchBackupsButton.getActionView();
// Set menu items visible
searchBackupsButton.setVisible(true);
setupSearchView(searchBackupsButton);
}
privatevoidsetupSearchView(final MenuItem searchItem)
{
if (isAlwaysExpanded())
{
searchView.setIconifiedByDefault(false);
}
else
{
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
searchView.setOnQueryTextListener(newOnQueryTextListener() {
@OverridepublicbooleanonQueryTextSubmit(String query)
{
// If you are using a pager view (tabbed) you can get the current one hereif(pager.getCurrentItem() == 0)
{
// do your searches here// Hide the keyboard
searchView.clearFocus();
}
// same as aboveelseif(pager.getCurrentItem() == 1)
{
// do your searches here// Hide the keyboard
searchView.clearFocus();
}
returntrue;
}
@OverridepublicbooleanonQueryTextChange(String newText)
{
// Here for each key pressed there will be a timer starting from X and drecrementing.// When the timer reaches zero it will perform a search// So, lets say the user inserted letter 'a'. 500ms later, if the user didnt input// anything else the search for query 'a' will be made.Log.d(TAG, "Query: " + newText);
// if there is already a count down running, cancel it and start a new oneif(isCountDownTimerRunning)
{
countDownTimer.cancel();
}
// There is nothing to search for, show the full list!// This is usefull when the user presses the cross 'x' on the search view// Then the full list will be shown again// Or when the user presses backspace until it clears the searchviewif("".equals(newText) || newText == null)
{
// check what current fragment is on the pagerif(pager.getCurrentItem() == 0)
{
// There is nothing to search for, show the full list!
}
// if cloud backupselseif(pager.getCurrentItem() == CLOUD_BACKUPS_FRAGMENT_POSITION)
{
// There is nothing to search for, show the full list!
}
}
else
{
// Get the active fragmentif(pager.getCurrentItem() == 0)
countDownTimer = timedSearchQuery(TIME_INTERVAL_BETWEEN_KEY_PRESSED_AND_SEARCH, newText, 0);
elseif(pager.getCurrentItem() == 1)
countDownTimer = timedSearchQuery(TIME_INTERVAL_BETWEEN_KEY_PRESSED_AND_SEARCH, newText, 1);
}
returnfalse;
}
});
}
privateCountDownTimertimedSearchQuery(long timeInMillis, final String searchFor, final int fragment)
{
countDownTimer = newCountDownTimer(timeInMillis, timeInMillis/2) {
@OverridepublicvoidonTick(long millisUntilFinished) {
}
@OverridepublicvoidonFinish() {
if(fragment == 0)
// do your search on fragment @ position zero;elseif(fragment == 1)
// do your search on fragment 2 position 1// and so on...
}
};
countDownTimer.start();
isCountDownTimerRunning = true;
return countDownTimer;
}
protectedbooleanisAlwaysExpanded() {
returnfalse;
}
Post a Comment for "Searchview Auto Submit After Couple Of Letters Or Seconds"