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.
Related
The search bar is supposed to be here:
And in run-time it goes here:
This is the code for my menu activity:
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/search"
android:layoutDirection="rtl"
android:title="#string/Search"
android:icon="#drawable/search_icon"
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
</menu>
And I'm inflating it on another activity like this:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_bar,menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
I also tried changing the gravity programatically like this:
searchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
Any help would be greatly appreciated, especially if it contains the reason as why it's not behaving as I expected since I'd really like to understand what's happening.
Thanks!
remove android:layoutDirection="rtl"
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 !
Solution:
The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.
I have a Fragment with ViewPager and the ViewPager has 3 Fragments. Each Fragment has a SearchView in its Menu Items (Displayed in the Toolbar as Menu Item). When i click on the SearchView, i get the click event in onOptionsItemSelected, but the SearchView doesn't expand. Not sure what to do.
I have set setHasOptionMenu(true) in onCreate() for all the Fragments.
Here is my current code: for each Fragment
onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_new, menu);
SearchManager searchManager = (SearchManager) activity.
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.ic_action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(activity.getComponentName()));
searchView.setOnQueryTextListener(this);
}
Menu XML 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/ic_action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="0"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item
android:id="#+id/action_list"
android:icon="#drawable/ic_action_list_white"
android:orderInCategory="1"
android:title="#string/action_list"
android:checked="true"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="#+id/action_grid"
android:icon="#drawable/ic_action_grid_white"
android:orderInCategory="2"
android:title="#string/action_grid"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="#+id/action_mini"
android:icon="#drawable/ic_action_grid_white"
android:orderInCategory="3"
android:title="#string/action_mini"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
</menu>
Other related code: if it helps
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
mViewPagerPetitionsRecyclerViewAdapter.getFilter().filter(newText);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_list) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
} else if (item.getItemId() == R.id.action_grid) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(2);
} else if (item.getItemId() == R.id.action_mini) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
}
ActivityCompat.invalidateOptionsMenu(activity);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
mIMainNewPetitionsListener.onLayoutChangedListener();
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem list_item = menu.findItem(R.id.action_list);
MenuItem grid_item = menu.findItem(R.id.action_grid);
MenuItem mini_item = menu.findItem(R.id.action_mini);
String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);
if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
list_item.setChecked(true);
grid_item.setChecked(false);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(true);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(false);
mini_item.setChecked(true);
}
super.onPrepareOptionsMenu(menu);
}
I think you have to expand the action view yourself:
In onOptionsItemSelected:
if (item.getItemId() == R.id.action_search) {
item.expandActionVew();
}
I am using Appcompat v7 23.1.1. I have a fragment with ViewPager(FragmentStatePagerAdapter) loading 3 fragments. I have a SearchView with in each of these 3 fragments. When i click on the SearchView, the view didn't expand.
The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.
Not sure if this is a bug, but this post and this post helped me in solving this.
I have created a menu for my actionmode bar with icons but not all menu are showing with icon in actionmode bar. This is my menu xml 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/menu_archive"
android:icon="#drawable/ic_action_file_archive"
android:orderInCategory="100"
android:title="#string/action_remove"
app:showAsAction="always" />
<item
android:id="#+id/menu_upload_to_cloud"
android:icon="#drawable/ic_action_file_cloud_upload"
android:orderInCategory="200"
android:title="#string/action_upload_to_cloud"
app:showAsAction="always" />
<item
android:id="#+id/menu_delete"
android:icon="#drawable/ic_action_file_delete"
android:orderInCategory="300"
android:title="#string/action_move_to_trash"
app:showAsAction="always" />
</menu>
This is my code for creating Actionmode Bar.
#Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_actionmode_device_documents, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
}
#Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
this.actionMode = null;
}
This image is my output which is showing only one icon of menu but i want all other icons too.
This may be a little too late, but I'm putting this answer in case someone else runs into the same problem. It seems the system does not keep count of the app:showAsAction="always" attribute.
The soulution is to update the menus manually in onPrepareActionMode
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.findItem(R.id.menu_archive).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_upload_to_cloud).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
This seems odd but it works.
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