Android Searchview not shown though keyboard is popped up - android

I have the code below for searchview implementation. I have written listener code for the searchview to dismiss a fragment, if it is visible. On tap of search icon, fragment gets dismissed correctly, but searchview does not show up, keyboard gets popped up though. On tapping search icon again, serch textview comes.
Is there any obvious issue with the code below. Am I missing something?
Any help is much appreciated
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if (null != mSearchView) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideOverLayFragments();
}
});
Code for search menu:
<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="com.tcs.agilebiz.activities.HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:checked="true"
android:orderInCategory="103"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search" />

You should initialize your SearchView like this
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

Related

Giving error on my searchbar after extending AppCompatActivity

After extending AppCompatActivity instead of Activity, my project is giving the following error:
java.lang.NullPointerException: Attempt to invoke virtual method
'void
android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)'
on a null object reference
and I followed this link[ this][1] but it did not work for me.
Please give me some suggestions on what I am doing wrong.
This is my code for search:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
and this is menu xml
<item
android:id="#+id/search"
android:app:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_search"
android:app:showAsAction="collapseActionView|always"
android:title="#string/Search"/>
Try using the custom app namespace for your actionViewClass too:
app:actionViewClass="android.support.v7.widget.SearchView"/>
You have to change actionview class of searchview to support and change schema in menu. like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:title="Search"
android:titleCondensed="false"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
android:orderInCategory="0"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Then in the code you have to get the searchview and use expand listent like this. Remember to use SearchView of support library.
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setQueryHint(mContext.getString(R.string.search_messages));
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
}
return true;
}
});

Wrong menuitem found when fragment changes in Android

I have the following menu in the main fragment that contains a list:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
app:actionViewClass="android.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
It has the necessary setting in the onCreate:
setHasOptionsMenu(true);
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu,inflater);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
if(searchView!=null)
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(this);
}
When I click an item, a new fragment opens and it has a different menu:
<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=".MainActivity">
<item android:id="#+id/action_Save"
android:title="#string/action_Save"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_save"
/>
</menu>
Its onCreateOptionsMenu:
getActivity().getMenuInflater().inflate(R.menu.menu_fragment_second, menu);
super.onCreateOptionsMenu(menu, inflater);
If I press Back:
if (getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
my first fragment opens and tries to find its SearchView.
It can't find it as the menu of the second fragment is checked and it finds only the Save item.
What should I do to make it work?
You need to call invalidateOptionsMenu() to reset your menu for the fragment.
I had to change how the menuitem was found.
Instead of this:
SearchView searchView = (SearchView) ((MenuBuilder)menu).getActionItems().get(0).getActionView();
I use now this:
SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
This way it works.

How to make SearchView on Toolbar always expanded?

I already define setIconified() and setIconifiedByDefault() to false but the SearchView menu item is not expanding by default. Here's how I implemented it:
View customTitle = getLayoutInflater().inflate(R.layout.toolbar_custom_title, null);
toolbar.inflateMenu(R.menu.menu_buddies);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
SearchView searchView = (SearchView) toolbar.getMenu().findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Search Buddies");
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
My menu_buddies:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
xmlns:pawesome="http://schemas.android.com/apk/res-auto"
android:title="#string/action_search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
pawesome:showAsAction="always|collapseActionView"
pawesome:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
You need to change the value of android:showAsAction to always. The SearchView's attribute android:iconifiedByDefault should be true.
Have you solved this yet?
Remove:
searchView.setIconified(false);
and keep only this line:
searchView.setIconifiedByDefault(false);
As well as showAsAction="always" in menu-xml.
This works for me, the searchview is expanded and can not be collapsed by any means.
None of the above answers worked for me.
Use this
SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("search");
menu.findItem(R.id.action_search).setActionView(searchView);

Search icon is getting displayed twice in Toolbar in Fragment

I am trying to implement a search functionality in my application.
I am trying to display search icon in toolbar, but instead of single search icon multiple icons are getting displayed. One icon is getting displayed from menu.xml file and other icon is getting displayed from the line setHasOptionsMenu(true);.
If I do not use "setHasOptionsMenu(true)" line then onOptionsItemSelectedMenu method will not get called, if I do not give search icon in menu.xml file then search icon will not get displayed. Please let me know how to come out of the issue. I am trying hard with no fruits. Please help me come out of this issue.
My current toolbar looks as shown in the image below:
My menu.xml file:
<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="com.blo.ifo.ifocusblogs.ActivityForFragments">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/search"
android:title="#string/search_label"
android:icon="#mipmap/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I had the same problem
my problem
and this was the solution that I've found:
add "menu.clear();" to "onPrepareOptionsMenu" in MainActivity
solution
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
getMenuInflater().inflate(R.menu.main,menu);
MenuItem itemSearch = menu.findItem(R.id.action_search);
mSearchView = (SearchView) itemSearch.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()));
mSearchView.setQueryHint(getResources().getString(R.string.searchProduct));
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
adapter.getFilter().filter("");
listview.clearTextFilter();
}else {
adapter.getFilter().filter(newText.toString());
}
return true;
}
});
return true;
}

How to collapse SearchView on backpressed using appcompat v7.?

I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.
menu file:
<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" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_action_sort"
android:title="#string/sort"
app:showAsAction="ifRoom"/>
</menu>
java file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?
Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my menu 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"
tools:context="com.yourapp.youractivity">
<item
android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/nav_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
This is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}
In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).
Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()

Categories

Resources