How to implement search widget in fragment with Recycler View? - android

I have a NavigationDrawer and in a fragment I implement a list with Recycler View.
Now I want to implement a search widget in this Recycler View.
How I can do that?
this is that i have until now.
the menu.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
the file.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
}

Related

Getting SearchView from MenuItem returns View and not SearchView

I'm trying to fetch my SearchView from the Toolbar within a Fragment.
Im infalting a menu item in the onCreateOptionsMenu.
Problem : searchItem.getActionView() returns a "View" and not a "SearchView". See code below
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = searchItem.getActionView(); // This one is red - "returns View, should return Searchview"
}
XML for menu item (menu_search.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="#string/search"
android:id="#+id/search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Try this :
MenuItem search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();

Action bar only shows one item next to search view

I want to have a search view and two buttons in the Action Bar. So I created menu/options_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#drawable/route"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#drawable/cancel"
android:showAsAction="always" />
</menu>
I am inflating it in code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
This is what I get:
As you can see, only the first item ("route") is shown.
I can make second item ("cancel") visible if I let iconify the search widget:
//searchView.setIconifiedByDefault(false);
However, I want to have search widget expanded and both items visible. Is it possible?
Update
I managed to make them visible by setting android:orderInCategory="1" for "Route" and "Cancel" and android:orderInCategory="2" for the search widget:
This is almost what I wanted. The issue is that I want search widget to go first (leftmost), and then these two items. Is it possible?
Update 2
I tried "recommended" way. I set for SearchView:
android:showAsAction="always|collapseActionView"
After I click on search icon, I get expanded view:
Two problems:
(Minor) Navigation UI occupies a room on the left thus reducing space available for SearchView.
(Major) Android icon appears as a part of navigation UI. Can I get rid of it?
Created one demo, check it :
menu.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:title="Search"
android:icon="#android:drawable/ic_search_category_default"
app:actionLayout="#layout/searchview"
app:showAsAction="always|collapseActionView"/>
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#mipmap/ic_launcher"
app:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#mipmap/ic_launcher"
app:showAsAction="always" />
</menu>
searchview.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
Edited Java Based on Requirement (Always open searchview) :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.expandActionView();
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
// This listener use for handle back press if you want to go back without collapsing searchview
MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
onBackPressed();
return false;
}
});
}
return true;
}
Screenshots :
I did it by setting a maximum width for the search view, equal to screen width, minus the action item width.
res/menu/search.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:icon="#drawable/ic_search_white_24dp"
android:title="#string/title_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_filter_list_white_24dp"
android:title="#string/title_filter"
app:showAsAction="always" />
</menu>
Activity or fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.setMaxWidth(getSearchViewMaxWidth());
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
performSearch("");
return true; // Disable dismissing the search view
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
performSearch(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
#Px
private int getSearchViewMaxWidth() {
final int screenWidth = getResources().getDisplayMetrics().widthPixels;
final int actionItemWidth = getResources().getDimensionPixelSize(R.dimen.action_item_width);
return screenWidth - actionItemWidth;
}
res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- ... -->
<dimen name="action_item_width">48dp</dimen>
</resources>
Change your onCreateOptionsMenu as follows to explicitly set the menu items visible when the SearchView menu item has focus.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
if (menu.getItem(0).isFocused()) //detect if search view has focus
{
//explicitly set menu items visible
menu.getItem(1).setVisible(true);
menu.getItem(2).setVisible(true);
}
return true;
}
you can customize width of expanded search view by this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
params.width=500;
searchView.setLayoutParams(params);
menu.findItem(R.id.action_search).expandActionView();
return true;
}
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:orderInCategory="1"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#drawable/route"
android:orderInCategory="2"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#drawable/cancel"
android:orderInCategory="2"
android:showAsAction="always" />
</menu>

Regards search view in android

I implemented search view for filtering items in listview. It's working fine. My problem is search view showing > ( next icon ) in action bar. What is this? how can i remove this?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return true;
}
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_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>

Remove searchView icon

In my app, I'm using a SearchView in the actionbar that it's always expanded like this:
My main.xml is this:
<item
android:id="#+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="always"
android:title="Search" />
MainActivity.java:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchViewItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
return true;
}
I would like to remove the search icon and expand the editText.
Regards

changing the position of the search icon

I use SearchView in OptionsMenu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/icon_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
In main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
View inflatedView = getLayoutInflater().inflate(R.layout.arrow, null);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);
return (super.onCreateOptionsMenu(menu));
}
The search icon located on the right in menu. How to change its position in my menu? I want that it is located left.

Categories

Resources