I have implemented searchview in actionbar and it is working fine. In viewpager if user switch between fragment I want to keep the state of searchview, to do so whenever fragment switch I applied query and show search result in recycler until now it is working like charm except I want to expand my searchview and put a query into it on fragment change method. I have seen lots of thread on it and I have applied multiple solutions none worked.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_black_24dp"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
In onCreateOptionsMenu i am inflating my menu.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main2, menu);
this.menu = menu;
searchActionBar(menu);
super.onCreateOptionsMenu(menu, inflater);
}
In search Actionbar i am fetching query result and save query text to mQuery and clear this text when searchview collapse.
private void searchActionBar(Menu menu){
searchViewItem = menu.findItem(R.id.action_search);
searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
searchViewAndroidActionBar.setIconifiedByDefault(false);
searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#SuppressLint("RestrictedApi")
#Override
public boolean onQueryTextSubmit(String query) {
searchViewAndroidActionBar.clearFocus();
mQuery = query;
fetchSearchResult(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
MenuItemCompat.setOnActionExpandListener(searchViewItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mQuery="";
return true;
}
});
}
This function is called when fragment switch in viewpager , if there is any query I want to set in searchview. But searchview doesn't expand. Although query submit function is running.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isResumed() && isVisibleToUser) {
if (searchViewAndroidActionBar != null) {
searchViewAndroidActionBar.setFocusable(true);
searchViewAndroidActionBar.setQuery(mQuery,true);
}
}
}
Any help would be appreciated.
Related
I'm trying to implement searchview on my fragment toolbar. For me other menu item is working properly but using searchview id is not either clicked or listeners invoked to perform searching. I'm showing few lines below.
crop_searchview_menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/crop_search_menu"
android:icon="#drawable/ic_search_icon"
android:title="#string/search_crop_title"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
<item
android:id="#+id/searchview_addcrop_menu"
android:title="#string/add_crop_btn"
app:showAsAction="ifRoom"
/>
</menu>
Here in above code the second id="searchview_addcrop_menu" is working properly on performing onitemclick. I have added this menu on my MaterialToolbar which is under AppBarLayout as below.
cropMainToolbar :
<com.google.android.material.appbar.MaterialToolbar
app:menu="#menu/crop_searchview_menu"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
android:id="#+id/cropMainToolbar"
app:title="#string/crops_home_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content">
I have applied onMenuItemClickListener on my toolbar because onCreateOptionsMenu() is not working for me inside fragment by any methods shown here which shows problem as my inflated menu is never shown on my toolbar.
I skipped that and tried to below in java code.
OnMenuItemClickListener code:
cropMainToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId()== R.id.crop_search_menu){ //not working
SearchView searchView= (SearchView) cropsMenu.findItem(R.id.crop_search_menu).getActionView();
searchView.setQueryHint(getResources().getString(R.string.search_crop_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
System.out.println("Typed "+newText); //not working
return false;
}
});
}if(item.getItemId()==R.id.searchview_addcrop_menu){ //works
Intent intent = new Intent(context, AddChangeCropActivity.class);
cropChangesLaucher.launch(intent);
}
return true;
}
});
Please help me to perform searchview execution. I'm using Android Studio 2021.1.1 gradle 7.0
You need to do the initialize SearchView in the onCreateOptionsMenu() function. Your initialization should go like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView searchView= (SearchView) cropsMenu.findItem(R.id.crop_search_menu).getActionView();
searchView.setQueryHint(getResources().getString(R.string.search_crop_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
System.out.println("Typed "+newText); //not working
return false;
}
});
return true;
}
I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Trying to include a searchview in actionbar. For this, I have done the following:
Created MenuSearch.xml in the menu folder as given below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_search"
android:actionViewClass="Android.Support.V7.Widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="Search"/>
</menu>
Included the method OnCreateOptionsMenu in the fragment:
public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
{
inflater.Inflate(Resource.Menu.MenuSearch, menu);
base.OnCreateOptionsMenu(menu, inflater);
var searchManager = (SearchManager) this.Activity.GetSystemService(Context.SearchService);
searchView = (SearchView) (menu.FindItem(Resource.Id.action_search).ActionView);
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));
}
And I have set HasOptionsMenu = true; in oncreate of the fragment.
With the code above, I get null pointer in the line searchView.SetSearchableInfo... . Not sure why this happens, though everything seem to be correct and in place.
Based on some suggestions for resolving similar issue, I have also tried out the below option for searchview.
var item = menu.FindItem (Resource.Id.action_search);
var searchItem = MenuItemCompat.GetActionView(item);
searchView = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();
searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));
But again, I get searchView as null.
Now that I have tried the possible options, I am clueless as to why searchview becomes null.
Any help is much appreciated.
Change your MenuSearch.xml to
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/app_name"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
And in your fragment, add searchView.setOnQueryTextListener
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.search_option_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
// MenuItemCompat.setShowAsAction(item, //MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | //MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
// MenuItemCompat.setActionView(item, searchView);
// These lines are deprecated in API 26 use instead
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setActionView(searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
}
Answer updated for 2018. Sorry for Kotlin, if you're using Java.
Your xml menu resource (search_menu.xml for example):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/app_name"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
Build-up code:
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
super.onCreateOptionsMenu(menu, inflater)
menu.clear()
inflater?.inflate(R.menu.search_menu, menu)
val searchView = SearchView((context as MainActivity).supportActionBar?.themedContext ?: context)
menu.findItem(R.id.action_search).apply {
setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW or MenuItem.SHOW_AS_ACTION_IF_ROOM)
actionView = searchView
}
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
return false
}
override fun onQueryTextChange(newText: String): Boolean {
return false
}
})
searchView.setOnClickListener {view -> }
}
You'll probably also need to put this line in onCreate() (worked for my fragment) to get this all to appear:
setHasOptionsMenu(true)
For late 2018 and for apis >= 26 compatible (deprecation warnings) and a properly formatted answer:
The menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="Search_change_me_with_proper_id"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"/>
</menu>
Java code inside the ActivityFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true); // Add this!
return inflater.inflate(R.layout.fragment_add_app, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_add_app, menu);
MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// Here is where we are going to implement the filter logic
return true;
}
});
}
OR as a second option you let the ActivityFragment implement SearchView.OnQueryTextListener and then use the menu search:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true); // Add this! (as above)
return inflater.inflate(R.layout.fragment_add_app, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.menu_add_app, menu);
MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(this); // Fragment implements SearchView.OnQueryTextListener
}
#Override
public boolean onQueryTextChange(String query) {
// Here is where we are going to implement the filter logic
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
i solved this issue i wants to share my answer for searchView NullPointerException
we need to remove below method from Main Activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return true;
and use inside fragment in OnActivityCreated setHasOptionsMenu(true)
and Override methods
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_search:
// onCall(); //your logic
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Your search_options_menu.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="#string/app_name"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
/>
</menu>
Override onCreateOptionMenu method in your fragment setHasOptionsMenu(true); inside onCreate() on your fragment.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.search_option_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Do search code here
return true;
}
});
}
search_view.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(query: String?): Boolean {
adapter.filter.filter(query)
return true
}
})
And don't add the intent filter for your activity in Manifest file.
In my fragment's onCreateOptionsMenu the code
SearchManager searchManager =
(SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
searchManager.getSearchableInfo(getActivity().getComponentName());
searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo( searchableInfo);
produced following error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo (android.app.SearchableInfo)' on a null object reference
After i changed in menu.xml android:actionViewClass to app:actionViewClass the problem did not occur again.
menu.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="...">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom">
I've recently updated my apps to appcompat to v7:22.2.0. I have a searchview in the actionbar which was working fine before updating, but now the search field appears under the title when i press the search icon.
This is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_participants"
android:icon="#drawable/search_white"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always"/>
</menu>
It still works, its fully functional, only the actionbar title is still displayed when using the SearchView. How can i prevent this from happening? Should i manually set the title to an empty string or is there a decent fix for this?
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mOptionsMenu = menu;
if (mOptionsMenu != null) {
menu.clear();
mMenuInflater.inflate(R.menu.live_tracking_overview_menu, menu);
mSearchItem = menu.findItem(R.id.search_participants);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint(getString(R.string.search));
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
showSearch();
//i could hide the title here, but that's an ugly fix
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
hideSearch();
return true;
}
});
Background
I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.
The problem
Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.
As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.
What I've tried
I've tried using each of the next methods, but none worked:
MenuItemCompat.setOnActionExpandListener.
MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.
setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.
SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).
I've also tried to mess around with the XML file of the search menu item.
The code
Here's the helper class I've made:
SearchHolderCompat
public class SearchHolderCompat {
public MenuItem mSearchMenuItem;
public SearchView mSearchView;
private final Activity _context;
public SearchHolderCompat(final Activity context) {
_context = context;
}
public boolean isCurrentyExpanded() {
return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
}
public boolean hasQuery() {
return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
&& !TextUtils.isEmpty(mSearchView.getQuery());
}
public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
final MenuInflater menuInflater = _context.getMenuInflater();
menuInflater.inflate(R.menu.search_menu_item, menu);
init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
}
public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
this.mSearchMenuItem = searchMenuItem;
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
if (mSearchView == null) {
MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
}
mSearchView.setQueryHint(_context.getString(R.string.search));
mSearchView.setOnQueryTextListener(onQueryTextListener);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(final String arg0) {
android.util.Log.d("AppLog", "onQueryTextSubmit");
return false;
}
#Override
public boolean onQueryTextChange(final String queryText) {
android.util.Log.d("AppLog", "onQueryTextChange");
return true;
}
}, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return false;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return false;
}
});
return true;
}
}
search_menu_item.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- search items -->
<item
android:id="#+id/menuItem_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction"/>
</menu>
The question
What's the correct way to handle the SearchView and the search menu item (using the support library) ?
How come "MenuItemCompat.setOnActionExpandListener" doesn't work?
After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)
.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
if(searchItem != null)
{
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// use this method for search process
searchView.setOnSearchClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Search view is expanded
showSearchPage();
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener()
{
#Override
public boolean onClose()
{
//Search View is collapsed
hideSearchPage();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
#Override
public boolean onQueryTextSubmit(String query)
{
// use this method when query submitted
Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
// use this method for auto complete search process
Log.e("SearchValueIs",":"+newText);
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
Hope It will help Someone...
Android training
app:showAsAction="ifRoom|collapseActionView"
You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609
You can see below how to handle Expand and Collapsed states!
To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).
This is the copy of my answer form the other question:
I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:
searchItem
.setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
But this is changing the SearchView and is replacing the DrawerToggle with back arrow.
I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.
Solution:
When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.
menu_search.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
</menu>
In the activity:
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
..........
private View mSearchEditFrame;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
searchView.setSubmitButtonEnabled(false);
mSearchEditFrame = searchView
.findViewById(android.support.v7.appcompat.R.id.search_edit_frame);
ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
int oldVisibility = -1;
#Override
public void onGlobalLayout() {
int currentVisibility = mSearchEditFrame.getVisibility();
if (currentVisibility != oldVisibility) {
if (currentVisibility == View.VISIBLE) {
Log.v(TAG, "EXPANDED");
} else {
Log.v(TAG, "COLLAPSED");
}
oldVisibility = currentVisibility;
}
}
});
return super.onCreateOptionsMenu(menu);
}
I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.
#Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return true;
}
In more recent versions
menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
//SearchView appers
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//SearchView disappears
return true;
}
});