I have an app with a NoActionBar theme. In my main activity I have an options menu that I created manually on the top of the screen (or by utilizing the built in device's options button).
In this main activity, I have a fragment with a listView where I apply the action mode long click functionality, to show the contextual action bar (CAB) for further user options.
Now, I try adding an options item to my CAB so it will contain some options like selecting all items in the listView, but since it's an item of the CAB, I can't really show the popup menu like in a regular activity. Further more, I want all the options menu callbacks (such as onOptionsItemSelected) to stay in the context of the CAB, in order to be able to continue to perform actions on the CAB.
Here's the code of my CAB:
<?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_delete"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_overflow"
app:showAsAction="always"
android:orderInCategory="200"
android:icon="#drawable/ic_action_overflow"
android:title="Options"
android:visible="false"/>
</menu>
Apparently I missed the built in feature of the CAB - a built in overflow menu that collapses some of the actions' items once the screen is too small to show them all.
Another manipulation that needs to be done in order to always collapse certain actions under that overflow menu is to set for each one of them:
android:showAsAction="never"
app:showAsAction="never"
So, say we have 3 actions (delete, selece_all, add) in the CAB, and we want two of them (select_all, add) to be collapsed always under the built in overflow menu, we'll set this in the CAB's xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mm="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_delete"
android:orderInCategory="100"
mm:showAsAction="always"
android:icon="#drawable/ic_action_delete"
android:title="Delete"/>
<item
android:id="#+id/action_select"
android:orderInCategory="200"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/select_all"/>
<item
android:id="#+id/action_add"
android:orderInCategory="300"
android:showAsAction="never"
mm:showAsAction="never"
android:title="#string/button_add"/>
Related
I have a class that extends FragmentActivity. No I want to show my menu items at the top of the screen like in actionbar. How can I do this ?
I have created the menu items and they are visible hen the hardware button for menu is pressesd but I want to sho them at the top of the screen.
Basically, this happens in devices with physical menu button. In devices without that button, all options are displayed in the action bar.
You can force a menu item to be displayed in the action bar by addind "showAlways" to the item that you want.
showAsAction=“always”
An small example
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" />
</menu>
You must need to ensure that you have enough space for the menu items.
To ensure that I'll have enough space, I use to create a menu with only one item. Then, inside this item, I create another menu. This way, only one button is displayed and all items appear in a floating pop up menu. The main idea is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/overflow_item"
android:icon="#drawable/ic_overflow_icon"
android:title="#string/options"
android:showAsAction="always" />
<menu>
<item android:id="#+id/action_clear"
android:title="#string/clear"
android:showAsAction="always" />
</menu>
</menu>
Im working on action menu item and its over flow item this is my main_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/menu_search"
android:icon="#drawable/search"
android:title="#string/start_new_project"
app:showAsAction="always" />
<item
android:id="#+id/menu_dts_overflow"
android:icon="#drawable/ic_action_overflow_round"
android:orderInCategory="11111"
android:title="Service"
app:showAsAction="always">
<menu>
<item
android:id="#+id/menu_newProject"
android:icon="#drawable/newproject"
android:title="#string/start_new_project"
app:showAsAction="never" />
<item
android:id="#+id/menu_help"
android:icon="#drawable/help"
android:title="Service Tasks"
app:showAsAction="never" />
<item
android:id="#+id/menu_signOut"
android:icon="#drawable/signout"
android:title="#string/menusignout"
app:showAsAction="never" />
</menu>
</item>
I tried to construct a search item and a overflow item which you can see in the above code. I'm new to Action bar menu items so i tried to Google it and was able to make it work as I need.
In this I have to know one more thing.
1. What is orderInCategory with some numbers and what for it is used..?
android:orderInCategory is an integer attribute that dictates the order in which the menu items will appear within the menu when it is displayed.
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_first"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/string_one"/>
<item
android:id="#+id/menu_item_second"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/string_two"/>
</menu>
Menu items in ToolBar are arranged from left to right (or
start to end in RTL mode) in the ascending order (i.e. 1,2,3 ->
left to right).
Menu Items in Overflow menu are arranged from top to bottom in
the ascending order (i.e. 1,2,3 -> top to bottom).
android:orderInCategory is actually useful in two ways.
1. For menu items in ActionBar.
Items will appear from left to right in ActionBar depending on the ascending order.
2. For menu items in overflow menu.
Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.
android:orderInCategory Higher value, lower priority.
I have a Activity and a Fragment in it, both of them have option menu, and the item numbers are 1 and 3.
If I set android:orderInCategory=0, the activity menu are above the fragment menu, same effect before I set the value.
But if I set android:orderInCategory=1,the activity menu are below the fragment menu, and that is what I want.(I also test android:orderInCategory=5 tested too, still the same effect.)
A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).
So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.
Here's my menu:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_stores"
android:title="#string/action_stores"
android:icon="#drawable/ic_action_stores"
android:showAsAction="always" />
<item
android:id="#+id/action_shopping_lists"
android:title="#string/action_shopping_lists"
android:icon="#drawable/ic_action_lists"
android:showAsAction="always" />
<item
android:id="#+id/action_preferences"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
My searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?
The few lines of code within the onClickListener would contain something like this:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'.
You can change it back later in an appropriate method.
I have an app (for 4.0 and above) which has couple of MenuItems in the ActionBar (one with 'always' and the rest with 'ifRoom' or 'never' property).
So the issue goes like this...
If I launch the app in landscape mode, the ActionBar looks like the following:
When I rotate to landscape from portrait, the ActionBar looks like this:
As you can see, more icons appear than they should be, i.e no space is left for title bar and the tabs menu (Navigation Menu). Actually, the 4 items are shown in the portrait mode (it's a split ActionBar) and it seems that the Android doesn't realise that there's no SplitActionBar there anymore.
Please suggest what I can do to fix this? :)
Edit
My menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share"/>
<item
android:id="#+id/action_viewToggle"
android:icon="#drawable/ic_menu_view_as_grid"
android:showAsAction="ifRoom"
android:title="#string/switch_view"/>
<item
android:id="#+id/action_upload"
android:icon="#drawable/ic_menu_upload"
android:showAsAction="ifRoom"
android:title="#string/upload"/>
<item
android:id="#+id/action_newFolder"
android:icon="#drawable/ic_menu_new_folder"
android:showAsAction="ifRoom"
android:title="#string/create_folder"/>
<item
android:id="#+id/action_multiselect"
android:icon="#drawable/ic_menu_select_all"
android:showAsAction="ifRoom"
android:title="#string/selection_mode"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom"
android:title="#string/refresh"/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_menu_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Make sure you take care of each menu item visibility in your menu.xml file, check out the xml ShowAsAction attribute.
here's an example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/SOME_MENU_ITEM"
android:icon="#drawable/SOME_ICON"
android:showAsAction="ifRoom" | "never" | "withText" | "always" | "collapseActionView"
android:title="SOME_TITLE"/>
.
.
One of the important attribute xml for menu items is,
android:showAsAction ,that defines the visibility of the action item.
From your code it is evident that if there is room in action bar it'll display it.
So if you don't want it to appear specify android:showAsAction value as never so that whatever the orientation is it'll appear only in the overflow menu.
In my action bar I have 2 different icons that load different data. I want to remove my popup dialog's when the data is loading and replace the icon in the action bar with an indeterminate spinner whilst the data's loading, and then go back to the original icon.
What will be the best way of doing this? Will I need a custom action bar layout? At the moment my action bar icon's are just set up from a XML file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings"
android:title="#string/clear_stored_data"
android:orderInCategory="100"
android:icon="#drawable/content_discard"
android:showAsAction="never" />
<item android:id="#+id/refresh_list"
android:title="#string/refresh_list"
android:orderInCategory="100"
android:icon="#drawable/navigation_refresh"
android:showAsAction="always" />
<item android:id="#+id/search_using_location"
android:title="#string/search_using_current_location_short"
android:orderInCategory="100"
android:icon="#drawable/device_access_location_found"
android:showAsAction="always" />
You might want to take a look at the RefreshActionItem project on Github. It's a third party component that does exactly what you are looking for.