In my application, I have a screen which has a menu on action bar on tap of which I show sub-menus which I need to put at runtime depending on the status of a process running in background.
e.g. Suppose, I am on screen SCREEN and my menu is MENU which has three sub-menus
MENU----> 1. SubMenu1
2. SubMenu2
3. SubMenu3
If a process PROCESS running in background is finished I want to display menus SubMenu1 and Submenu2 otherwise I'll put only SubMenu3 excluding SubMenu1 & SubMenu2.
I can take this decision in onCreateOptionsMenu(Menu menu) method but if the user is on the SCREEN and the PROCESS completes, SubMenu3 should be shown instead of the previously shown SubMenus.
I believe this can be done (as Play Store app adds "My Apps" menu at runtime).
How can I achieve this?
You can implement onCreateOptionsMenu(Menu) and create the options menu there based on the current state.
Whenever that state changes you can call invalidateOptionsMenu() to indicate that the options menu needs to be recreated. This will recreate the menu and again call onCreateOptionsMenu(Menu) as described in http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu%28%29
Related
I need to program something like the image:
When I long click a item in a ListView two options appear in the toolbar, to delete the item or cancel the action.
I want to make exactly like that, I do not want a context menu (which I know how to do).
Is it possible to do this with simple code? If no, can I accomplish that manipulating the toolbar? How can I do that?
PS: I can only use native code.
You need to use contextual Action Mode Over Toolbar.For Using Action Mode we need to extend our class with ActionMode.Callback. Its a Callback interface for action modes. Supplied to startSupportActionMode(Callback) (Callback)}, a Callback configures and handles events raised by a user’s interaction with an action mode.You need to override following method.
onCreateActionMode(ActionMode, Menu) once on initial creation.
onPrepareActionMode(ActionMode, Menu) after creation and any time the ActionMode is
invalidated.
onActionItemClicked(ActionMode, MenuItem) any time a contextual action button is
clicked.
onDestroyActionMode(ActionMode) when the action mode is closed.
I got a menu with a single menu item. When I click the 3 dots menu icon in the action bar, a drop down list is opened to show the single item there is to choose.
How can I automatically handle that menu item as the user clicks the menu icon, without showing the drop down list (meaning the menu item should be chosen automatically for the user)?
You can use onMenuOpened to override the behavior:
onMenuOpened
added in API level 1 boolean onMenuOpened (int featureId,
Menu menu) Called when a panel's menu is opened by the user. This may also be called when the menu is changing from one type
to another (for example, from the icon menu to the expanded menu).
I want to make Option Menu for Android, I have visit this site. In their script, I found onPrepareOptionsMenu, I try to compile and run using Android 2.3.3 compiler with and without onPrepareOptionsMenu, both works, but I didn't see any difference.
public boolean onCreateOptionsMenu(Menu menu){
//code here
}
public boolean onOptionsItemSelected(MenuItem item){
//code here
}
public boolean onPrepareOptionsMenu(Menu menu){
//code here
}
What is actually onPrepareOptionsMenu method do? Is that method important? Could I just delete the method?
Addition
Oh, I also hear about Action Bar in Android 3.0, it says that Action Bar is the alternative way for make Option Menu, and it using onPrepareOptionsMenu. Is that right?
Thank you...
Take a look in the API:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
If you want to alter the menu before it's shown to the user, you can put code to do that into onPrepareOptionsMenu. I've used that dynamically to disable some menu options in some circumstances.
As an example of when one might want to disable a menu option, I had an app where there was a way of specifying a destination. One of my menu options was to calculate a route to the destination. However, if a destination wasn't specified, that option didn't apply, so I used onPrepareOptionsMenu to disable that menu option when it wasn't applicable.
From Android 3.0 and beyond, there's the ActionBar, which is a menu bar. The most important items go into the ActionBar itself, but then there's an overflow for when there's not enough room on the action bar. One can specify that menu items should always be in the overflow menu and never on the action bar itself. On some devices, the action bar overflow corresponds to the permanent menu button on the device, whereas on other devices which don't have a menu button the overflow menu is seen on the right hand side of the action bar as three vertical dots.
onCreateOptionsMenu is called once, when your activity is first created. If it returns false, no option menu is shown and onPrepareOptionsMenu is never called.
If onCreateOptionsMenu returns true, onPrepareOptionsMenu is also called before the activity is displayed, and also every time the options menu is invalidated. Use onPrepareOptionsMenu if you need to enable/disable, show/hide, or add/remove items after creating it.
If your menu does not change, use onCreateOptionsMenu.
example
#Override
public void onPrepareOptionsMenu(#NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
if(!URLUtil.isValidUrl(news.geturl())){
menu.findItem(R.id.share).setVisible(false);
}
}
For some reason, onCreateOptionsMenu() is called AFTER onResume() in my app... Therefore, I just can't get a hold of the menu while I'm setting up my UI (between onCreate() and onResume()), which results in not being able to setup the corresponding action items for my ActionBar...
The only work-around I've found so far is to manually call invalidateOptionsMenu() right before onCreate() returns; that way onCreateOptionsMenu() is immediately called, I get a hold of the menu and then I can finally add the desired action items.
Has anyone experienced this issue? How are you supposed to programmatically setup your action items given onCreateOptionsMenu() is called after onResume()?
My app is running on JellyBean, it uses the built-in ActionBar (no ActionBarSherlock), android:minSdkVersion="14" and android:targetSdkVersion="16"
First consider that perhaps you shouldn't be doing this. It sounds like your idea might go against typical design patterns for Android. If your menu is changing in response to a user selection, for example, you should use contextual action mode instead.
From the Action Bar API Guide:
As a general rule, all items in the options menu (let alone action items) should have a global impact on the app, rather than affect only a small portion of the interface. [...] So, even before deciding whether a menu item should appear as an action item, be sure that the item has a global scope for the current activity.
From the Menu API Guide:
You should never change items in the options menu based on the View currently in focus. When in touch mode (when the user is not using a trackball or d-pad), views cannot take focus, so you should never use focus as the basis for modifying items in the options menu. If you want to provide menu items that are context-sensitive to a View, use a Context Menu.
Barring that, if you do want to change the menu items as you have described, you should make the change in onPrepareOptionsMenu(). When the event occurs that requires changing the menu items, put the relevant information into a field and call invalidateOptionsMenu(). Override onPrepareOptionsMenu() and check the value of the field to determine which menu items to add/remove.
(It would also work to call invalidateOptionsMenu() and override onCreateOptionsMenu() to modify which menu items should be shown, although this approach is not recommended.)
More from the Menu API Guide:
You should use onCreateOptionsMenu() only to create the initial
menu state and not to make changes during the activity lifecycle.
If you want to modify the options menu based on events that occur
during the activity lifecycle, you can do so in the
onPrepareOptionsMenu() method.
This method passes you the Menu object as it currently exists so you
can modify it, such as add, remove, or disable items. (Fragments also
provide an onPrepareOptionsMenu() callback.)
On Android 2.3.x and lower, the system calls onPrepareOptionsMenu()
each time the user opens the options menu (presses the Menu button).
On Android 3.0 and higher, the options menu is considered to always be
open when menu items are presented in the action bar. When an event
occurs and you want to perform a menu update, you must call
invalidateOptionsMenu() to request that the system call
onPrepareOptionsMenu().
Can you please tell me the difference between context menu and option menu in android?
When I click the menu button on the emulator, is that option menu? or context menu?
And how to invoke the other menu (not trigger by the menu button)?
Thank you.
When I click the menu button on the
emulator, is that option menu?
Yes.
And how to invoke the other menu (not
trigger by the menu button)?
By long-tapping on whatever widget (if any) has a context menu.
The page on UI Guidelines and Menu Design in the Android documentation gives a good explaination of each of the types of menus.
Two line summary:
Options Menu - the menu you see when pressing the 'Menu' button
Context Menu - the menu shown when you press and hold an item.
Google provides an extensive summary of the different menu types in their documentation.
Excerpt:
Options menu and action bar
The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings."
If you're developing for Android 2.3 or lower, users can reveal the options menu panel by pressing the Menu button.
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Context menu and contextual action mode
A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.
When developing for Android 3.0 and higher, you should instead use the contextual action mode to enable actions on selected content. This mode displays action items that affect the selected content in a bar at the top of the screen and allows the user to select multiple items.