I am building a chat application just like Whatsapp. I have build the search functionality successfully in my application using the SearchView in the ActionBar. This is what I have done so far:
Instead of this I want the SearchView with navigation up and down arrows (like in Whatsapp) when I click on the search button. This is what I want to achieve:
Here is my code for the Search button:
// SearchView
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setQueryHint("Search...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
adapter.filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.filter(newText);
return true;
}
});
This is the xml file in my menu folder:
<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" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:showAsAction="never|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Please tell me how to achieve this. Thanks
Related
In an AppCompat activity with context action bar
Trying to add android.support.v7.widget.SearchView to the action mode menu
the searchView doesn't expand
using appcompat-v7:25.2.0
menu Resource file
<?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_dark_24dp"
android:title="#string/search_list"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
<item
android:id="#+id/item_two"
android:icon="#drawable/item_two"
android:title="#string/item_two"
app:showAsAction="ifRoom"
>
</item>
</menu>
Activity Theme
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionModeOverlay">false</item>
<item name="actionModeBackground">#color/colorPrimaryDark</item>
</style>
Activity code
private class SelectionActionModeCallBack implements ActionMode.Callback {
private SearchView mSearchView;
SelectionActionModeCallBack() {
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.book_selection_action_menu, menu);//Inflate the menu over action mode
mSearchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
startSearch(s);
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
mSearchView.requestFocus();
return true;
}
This contradicts the documentation
Contrary to the case when using Search View in action bar the desired collapsible effect for search view while having other items in the action mode
use
app:showAsAction="always"
without collapseActionView !
I don't know how to collapse a SearchView without a MenuItem and I don't know how to get a MenuItem from a ToolBar that isn't an ActionBar
It's easy enough to add a SearchView to a Toolbar:
Toolbar mToolbar = (Toolbar) toReturn.findViewById(R.id.toolbar);
mToolbar.inflateMenu(R.menu.search_menu);
menu/search_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"
android:icon="#drawable/ic_grey_search"
android:title="#string/filter"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I can get ahold of a SearchView and add an OnQueryTextListener() like so:
final SearchView searchView = (SearchView) mToolbar.findViewById(R.id.search);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
ToastUtils.makeText("Test", Toast.LENGTH_LONG);
//MenuItemCompat.collapseActionView(searchView);
return true;
}
#Override public boolean onQueryTextChange(String newText) {return false;}
});
But, without a MenuItem I cannot call MenuItemCompat.collapseActionView(searchViewMenuItem).
Can someone let me know how to get a MenuItem from a none-ActionBar Toolbar.
EDIT
I hoped that mToolbar.collapseActionView(); was the solution, but this has no visible effect.
Hold a refrence to SearchView -> mSearchView.
Then where ever u want to collapse it just call
mSearchView.onActionViewCollapsed();
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;
}
I'm having a problem with the alignment of the SearchView when it's expanded. For some reason, when collapsed it aligns rights, but when expanded it aligns left. I did everything folowing the ActionBarSherlock examples.
Here are two screenshots of the problem:
Screenshot collapsed http://img59.imageshack.us/img59/8976/.png
Screenshot expanded http://img577.imageshack.us/img577/9890/.png
I have yet to decide if I want to use ActionBar tabs. I don't want the searchView to hide them when expanded. But that's for another question. My main issue here is the alignment of the SearchView when exapanded. I want it to keep the right alignment no matter what.
Here's my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
searchMenuItem = menu.findItem(R.id.action_search);
refreshMenuItem = menu.findItem(R.id.action_refresh);
refreshMenuItem.setActionView(R.layout.refresh_progressbar);
searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
flightsListAdapter.getFilter().filter("");
return true;
}
});
searchView = (SearchView)searchMenuItem.getActionView();
searchView.setQueryHint(getResources().getString(R.string.search_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String query) {
flightsListAdapter.getFilter().filter(query);
return true;
}
});
searchView.clearFocus();
return true;
}
And here is my menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:title="#string/search"
android:icon="#drawable/ic_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
<item
android:title="#string/date"
android:icon="#drawable/ic_compose"
android:showAsAction="always" >
<menu>
<item
android:id="#+id/menuYesterday"
android:title="Yesterday"/>
<item
android:id="#+id/menuToday"
android:title="Today"/>
<item
android:id="#+id/menuTomorrow"
android:title="Tomorrow"/>
</menu>
</item>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
android:showAsAction="always"
android:title="#string/action_refresh" />
</menu>
Thank you for reading :)
Ok. Just for the sake of common knowledge, I'm answering my own question.
The trick was to remove "collapseActionView" on the SerchView. Once I removed that, the SearchView stays in place (to the right) when exanded.
I am trying to create list search view in shreyaloclistfragment. I am getting NullPointerException at folowing line
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
My method is
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate( R.menu.menu_search, menu);
System.out.println( "inflating menu");
final SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
showFilteredItems( newText );
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return;
}
Stack trace
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sears.syw.FragmentShopsTab.onCreateOptionsMenu(FragmentShopsTab.java:52)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
at
com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559 )
at
com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSh erlockNative.java:65)
at
If you are using the Appcompat library, maybe you forgot to add your own xmlns on your menu_search.xml.
As the example says:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
</menu>
You need to add the folowing:
<item android:id="+#id/..."
android:actionViewClass="android.widget.SearchView"
yourapp:actionViewClass="android.widget.SearchView" />
Source: https://developer.android.com/training/basics/actionbar/adding-buttons.html#XML
If you are using AppCompat library, then in your menu.xml, add
app:actionViewClass="android.support.v7.widget.SearchView"
instead of
app:actionViewClass="android.widget.SearchView"
i am using android studio
<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.app.myapp" >
<item
android:id="#+id/search"
android:title="Buscar"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
app:actionViewClass="android.widget.SearchView"/>
</menu>
found 100%
To reference a layout from a menu xml use the actionLayout attribute:
<menu>
<item android:id="#+id/menu_name"
android:actionLayout="#layout/your_layout"
android:showAsAction="always"/>
</menu>
Some times problem is with
setHasOptionsMenu(true);
Move it from oncreate or onviewcreate to on attached method and try it.
Check this answer
Either you are using wrong namespace or importing wrong class
https://stackoverflow.com/a/38702262/5374951