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) {
}
});
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 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 have seen this question asked many times, however, none of those solutions have helped me. My problem is that I have an ActionBar menu and I want its items to always be displayed on the ActionBar instead of the drop down menu.
I have tried actions suchs as "ifRoom", "always", etc. and they still show only with text on the drop down menu.
Menu: devotional_fragment_actions.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_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
app:showAsAction="always"/>
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_go_to_today"
android:title="#string/date_action"
app:showAsAction="always"/>
<item android:id="#+id/action_next"
android:icon="#drawable/ic_action_next_item"
android:title="#string/next_action"
app:showAsAction="always"/>
</menu>
On my fragment I have:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// action bar
inflater.inflate(R.menu.devotional_fragment_actions, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection of action bar
switch (item.getItemId()) {
case R.id.action_search:
showDatePickerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The menu works fine but is not displaying as I want it. Thanks
In each item use android namespace instead of app. You can even remove this namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto".
Try set it like in code below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
android:showAsAction="always"/>
</menu>
While i m trying to show 3 menu in overflow menu on sherlockactionbar but it not showing overflow icon , but when i press menu button from hardware it shows option at bottom of screen :
I m overriding menu through this in SherlockFragmentActivity
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_home, menu);
return true;
}
and in Menu xml i have also added android:showAsAction="never" property coding for that .xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:icon="#android:drawable/ic_menu_info_details"
android:showAsAction="never"
android:title="about"/>
<item
android:id="#+id/menu_settings2"
android:icon="#drawable/plusnew"
android:showAsAction="never"
android:title="Add"/>
<item
android:id="#+id/menu_settings3"
android:icon="#drawable/tem2"
android:showAsAction="never"
android:title="Done"/>
</menu>
If your Phone has a HardwareButton for the Overflowmenu, it wont display the software button because you don't need it.
In this Case:
Not a Bug, its a feature :)
Try,
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
SubMenu subMenu1 = menu.addSubMenu("Share");
subMenu1.add("Facebook").setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_share);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT); //Note the flag SHOW_AS_ACTION_ALWAYS
return true;
}
I have an activity that extends the ActionBarActivity that's included in the support package revision 18. I have a menu item that contains a submenu and it works fine when I load up the app. However, if I call supportInvalidateOptionsMenu() for some reason the submenu doesn't pop up any more. The related code would be the xml for the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search"
android:title="#string/menu_search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always|collapseActionView"/>
<item
android:id="#+id/menu_now_playing"
android:icon="#drawable/ic_nowplaying"
android:title="#string/menu_nowplaying"
myapp:showAsAction="always"/>
<item
android:id="#+id/menu_station_overflow"
android:icon="#drawable/ic_overflow"
android:title="#string/more"
myapp:showAsAction="always">
<menu>
<item
android:id="#+id/menu_favorite"
android:icon="#drawable/ic_favorite"
android:title="#string/favorite"/>
</menu>
</item>
</menu>
And then the code to create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return super.onCreateOptionsMenu(menu);
}
I should note that this problem occurs on Gingerbread devices but there are no problems on android 4.x. Does anyone have any idea what might be going on here?
Here is a work-around (as we had the same issue). Any menu items that need to be modified later, we put into instance variables, example:
private MenuItem stationMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
stationMenuItem = menu.findItem(R.id.menu_station_overflow);
return super.onCreateOptionsMenu(menu);
}
public void doStuff(boolean menuVisible) {
if (stationMenuItem != null) {
stationMenuItem.setVisible(menuVisible);
}
}
This is NOT an ideal solution, but something that will work till this is fixed. Changes to menu items SHOULD happen in onPrepareOptionsMenu(Menu menu) after calling supportInvalidateOptionsMenu()
Do not return super.onCreateOptionsMenu(menu); because that will always return false. Just return true.