Action bar only shows one item next to search view - android
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>
Related
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>
Android: menu displaying at screen bottom
I was trying to create a menu, and add some actions "ifRoom" using code below: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="#+id/menu_search" android:icon="#android:drawable/ic_menu_search" android:actionViewClass="android.widget.SearchView" android:showAsAction="ifRoom|collapseActionView" android:title="#string/search_description" /> <item android:id="#+id/menu_refresh" android:title="#string/menu_refresh" android:showAsAction="ifRoom|withText" /> <item android:id="#+id/menu_preference" android:title="#string/menu_preferences" android:showAsAction="never" /> </menu> But what i got is the menu is displaying at the bottom... I want it to be at top of the screen... Any idea what happened here? Thanks! Screenshot link: http://i.stack.imgur.com/knECg.png Below is the MainActivity.java onCreateOptionaMenu method: #Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); // Moved from onCreate -- Retrieve the Search View and configure/enable it SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) ); return true; }
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
How to make the Action Bar SearchView fill Entire action bar?
I was trying to create an action bar search in my application, but in the expanded state the SearchView is not taking the entire action bar width( it is still showing other actions)! So, how to make the SearchView fill the full ActionBar Area (as in GMAIL app)?
None of the above solutions worked for me. Setting the MaxWidth of the SearchView worked for me: #Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_search, menu); SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView(); searchView.setMaxWidth(Integer.MAX_VALUE);
Got it #Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu); MenuItem searchViewItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) searchViewItem.getActionView(); searchView.setIconifiedByDefault(false); ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); searchView.setLayoutParams(params); searchViewItem.expandActionView(); return true; } <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=".MySwipeTabbedActivity" > <item android:id="#+id/action_search" android:title="Search" android:icon="#android:drawable/ic_menu_search" android:showAsAction="always|collapseActionView" android:actionViewClass="android.widget.SearchView" /> <item android:id="#+id/action_share" android:title="Share" android:icon="#android:drawable/ic_menu_share" android:showAsAction="ifRoom" /> <item android:id="#+id/action_settings" android:title="Settings" android:icon="#android:drawable/ic_menu_preferences" android:showAsAction="never" /> </menu> [Optional] Avoid the searchView from collapsing: searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { #Override public boolean onMenuItemActionExpand(MenuItem item) { return true; } #Override public boolean onMenuItemActionCollapse(MenuItem item) { return false; //never collapse } });
The Answer given by #Hiep is correct i followed those steps to get my solution.but i was using ActionbarCompact lib so it take some changes to made so that i get it to work This solution is only the modified answer of Hiep if you are using appcompact <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:materialdesign="http://schemas.android.com/apk/res-auto" > <item android:id="#+id/action_search" android:title="search" android:icon="#drawable/abc_ic_search_api_mtrl_alpha" materialdesign:showAsAction="always|collapseActionView" materialdesign:actionViewClass="android.support.v7.widget.SearchView" /> </menu> And in the Main Class onCreateOptionsMenu #Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); searchView.setLayoutParams(params); searchView.setIconified(false); MenuItemCompat.expandActionView(searchItem); return super.onCreateOptionsMenu(menu); } Avoid the searchView from collapsing:If u use the above method in Appcompact it will create a crash so use this solution to avoid that. MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() { /* (non-Javadoc) * #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem) */ #Override public boolean onMenuItemActionExpand(MenuItem item) { return true; } /* (non-Javadoc) * #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem) */ #Override public boolean onMenuItemActionCollapse(MenuItem item) { return false; } }); Thank you
Try This Only will work and keep all other items set to ifRoom <item android:id="#+id/action_search" app:actionViewClass="android.support.v7.widget.SearchView" android:title="#string/action_search" app:showAsAction="always"/>
Yes, I'm really late :) But I would like to share one more alternative solution. The idea is just to hide the grouped menu items when the SearchView gets expanded. Step 1: Group your menu items. Please make sure showAsAction of search view item is app:showAsAction="collapseActionView|always" <group android:id="#+id/myMenuGroup"> <item android:id="#+id/actionSearch" android:icon="#drawable/ic_search" android:title="#string/search" app:actionViewClass="android.widget.SearchView" app:iconTint="#color/textColorVariant2" app:showAsAction="collapseActionView|always" /> <item android:id="#+id/actionFilter" android:icon="#drawable/ic_filter" android:orderInCategory="0" android:title="#string/filter" app:iconTint="#color/textColorVariant2" app:showAsAction="ifRoom" /> ..... </group> Step 2: Get the menu reference in onPrepareOptionsMenu() callback override fun onPrepareOptionsMenu(menu: Menu?): Boolean { super.onPrepareOptionsMenu(menu) this.menu = menu // this.menu is a global scoped variable return true } Step 3 Add MenuItem.OnActionExpandListener into your activity and implement onMenuItemActionExpand() and onMenuItemActionExpand() like below override fun onMenuItemActionExpand(p0: MenuItem?): Boolean { menu?.setGroupVisible(R.id.myMenuGroup, false) return true } override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean { menu?.setGroupVisible(R.id.myMenuGroup, true) return true } This approach will expand the SearchView completely inside the ActionBar even if there are other menu icons present
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.