Search View with collapseActionView|always does not work on Action Menu - android

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 !

Related

Action mode submenu and checkableBehavior

I have a strange issue with submenus and android:checkableBehavior="single". It works fine if the menu is in action bar, but displays check boxes instead of radio buttons if menu is in the action mode. I use AppCompatActivity and create action mode with startActionMode().
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/priority"
android:title="#string/priority"
app:showAsAction="ifRoom">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/low_priority"
android:title="#string/low_pririty"/>
<item
android:id="#+id/normal_priority"
android:title="#string/normal_priority"/>
<item
android:id="#+id/high_priority"
android:title="#string/high_priority"/>
</group>
</menu>
</item>
</menu>
How can I fix this?
You menu works fine for me using the startSupportActionMode method instead of the startActionMode method. The startActionMode method should not be used when using the support library AppCompatActivity.
.startSupportActionMode(new android.support.v7.view.ActionMode.Callback() {
#Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.test_menu, 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) {
return false;
}
#Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
}
});

All icons of ActionMode Bar are not showing in Android?

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.

MenuItemCompat.getActionView(searchItem) return null

I am trying to add actions items in my action bar using appcompat. I added search action when I click on it my application crash with NullPointerException. In onCreateOptionsMenu I am getting this view reference but there I am always getting null. Any suggestion to resolve this ??
main.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_bar_refresh"
android:icon="#drawable/ic_action_refresh"
app:showAsAction="ifRoom"
android:title="#string/action_refresh" />
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="#string/action_search" />
<item
android:id="#+id/action_bar_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_bar_search:
mSearchView.setIconified(false);
return true;
}
return false;
}
}
Here I am getting null value in searchView
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
Screenshot
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"
android:title="#string/action_search" />
You change showAsAction attribute.
I fixed issue by setting Application Theme to Theme.AppCompat.Light
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">

menu.findItem returns null

I can't find the solution please help
The implements:
public class VentasActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, android.support.v7.widget.SearchView.OnQueryTextListener
where returns null:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.sales, menu);
restoreActionBar();
// HERE RETURNS NULL
searchView = (SearchView) menu.findItem(R.id.action_product_search).getActionView();
if (searchView != null) {
final Menu menu_block = menu;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// collapse the view ?
menu_block.findItem(R.id.action_product_search).collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
// listAdapter.getFilter().filter(query);
return false;
}
});
Log.i("sales module", "SearchView OK");
} else
Log.i("sales module", "SearchView is null");
return true;
}
return super.onCreateOptionsMenu(menu);
}
and sales.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_product_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
Many many thanks!
UPDATED: the code of restoreActionBar()
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
finally i found the problem:
I changed the last line in my /res/menu/sales.xml from:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="#+id/search_view_productos"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="#+id/search_view_productos"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
</menu>
Problem was the xml namespace on actionViewClass
for me cause of issue was wrong import:
android.widget.SearchView
instead:
androidx.appcompat.widget.SearchView
In my case, i was using vector image for "android:icon". I resolved it by using custom layout for menu item.
In my case, I added app:actionViewClass="android.widget.SearchView" attribute to my <item> tag

SearchView wrong alignment

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.

Categories

Resources