Searchview's Oncloselistener Doesn't Work

SearchView's OnCloseListener doesn't work

I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
Log.d("*******","onMenuItemActionExpand");
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
Log.d("*******","onMenuItemActionCollapse");
return true;
}

SearchView.OnCloseListener does not get invoked

Ok. i got the mistake. We cant add a searchCommand and do

setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW)

Doing this would remove the searchText if any and hence we cant do a onClose().

searchview oncloselistener not working

I had the same issue last week! To my surprise, you don't set the listener to the SearchView, you actually set it to the menu item that expands it.
This is exactly what I had to do in onCreateOptionsMenu:

        item = menu.findItem(R.id.friend_fragment_search_icon);
searchView = (SearchView) MenuItemCompat.getActionView(item);

// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//the searchview has been closed
return true; // Return true to collapse action view
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
mBtnFindFriends.hide();
return true; // Return true to expand action view
}
});

The friend_fragment_search_icon is the menu icon that opens the SearchView.
menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dingding="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/friend_fragment_search_icon"
android:title="@string/search_title"
android:icon="@drawable/ic_search_white_48dp"
dingding:showAsAction="always|collapseActionView"
dingding:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

SearchView's onClose listener never runs

Ended up:

   View searchViewsCloseBtn = searchView.findViewById(R.id.search_close_btn);
searchViewsCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toaster.show(myContext, "I hate this platform.");
}
});

"R.id.search_close_btn" is the actual reference to the SearchView's close btn.

If you manually set an onClickListener it works.

Android ActionBarSherlock, SearchView `setOnCloseListener` is not working

Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu

//        searchView.setOnCloseListener(new OnCloseListener() { 
// @Override
// public boolean onClose() {
// adapter.getFilter().filter("");
// Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show();
// return false;
// }
// });

MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
adapter.getFilter().filter("");
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
});

on close listener for android.support.v7.widget.SearchView

Try this:

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_LONG).show();
show_all_drugs();
return true;
}

SearchView does not respond to clicks after query submit

I found a half-answer to this problem. If I clear the focus of the searchView when the query is submitted, then when the user next taps the searchView, the OnQueryTextFocusChangeListener gets a call. It doesn't exactly answer my original question, but it's an acceptable workaround for my case.

ActionBar search view, how to handle left arrow click?

// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});

This is from here:https://stackoverflow.com/a/18186164/4200187



Related Topics



Leave a reply



Submit