I'd like a logout button to appear on my app's action bar only on a certain layout, for example, web.xml. When login.xml is the current view, I don't want the button there. Is this possible?
I currently have the button fully functioning, by utilizing the normal main_activity_actions method, but I can't figure out how to change what ones show depending on current content view.
Inside of the Fragment or Activity that uses the web.xml layout, you would need to override the onCreateOptionsMenu(Menu menu) method. There you can inflate a menu.xml with the desired icon and properties for the button you would like in the action bar.
To receive click events from this button, you would need to override the onOptionsItemSelected(MenuItem item) then ensure that the clicked button was yours and do your logic.
More details can be found here on the Google Dev page
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.
How to hide 3 dots from Navigation header which comes in the right of header? This could be repeated question. I found few similar questions and their answers but they were for older version of Android. I am using Android sdk 21.
Any idea how to hide that 3 dot button?
Just Remove Override Method like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_and_add, menu);
return true;
}
This Override Method is responsible to for creating three dote as you mention it's really OptionMenu. If you don't want it, don't override onCreateOptionsMenumethod.
Alternative
Don't Inflate the menu xml. Just block the line like this
//getMenuInflater().inflate(R.menu.search_and_add, menu);
other code can remain same. No problem at all..
Those "3 Dots" are the "Overflow" menu, and is created when you establish a menu using a file in the menu resources directory.
If you have buttons or functionality you are wanting to expose via you action bar, you will need to have the overflow buttons (or instead, you can choose to have your buttons exposed at the top level inside the Action bar.
If you really don't want a menu, get rid of the menu.xml file describing this menu, and then get rid of the onCreateOptionsMenu() from your Activity.
Here are the official docs, which describe how this works.
I think you are speaking about the options menu, to get rid of it remove the override of the method onCreateOptionsMenu
In your menu folder the xmlfile that is used by your activity, change the app:showAsAction="never" to app:showAsAction="always" or some other you can see the options that are availabe by pressing ctrl+space.
Or else to get rid of it completely just remove the whole code and it's corresponding usages.
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);
}
}
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
When an option menu is visible, any user action will hide it. How to prevent menu from being hidden as a result of any onTouchEvent outside menu borders ?
Use either
setGroupVisible(int group, boolean visible)
Show or hide all menu items that are in the given group.
or
setVisible(boolean visible)
Sets the visibility of the menu item.
Anyway, I think don't get exactly what you mean with menu, if this doesn't help comment and I'll give you more clues.