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.
SearchView>(); searchView. SetSearchableInfo(searchManager. GetSearchableInfo(Activity. ComponentName));
Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With