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);
}
}
Related
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 have an android app that has a side menu and the main body of content is inside a fragment. So far there are 3 fragments (Home, Settings, Help). What I am trying to do is have the menu in the top right only appear in the home fragments.
Using
this.setHasOptionsMenu(false);
doesn't do anything so I am obviously using that wrong. Any ideas on how I can accomplish this?
Use:
menu.findItem(R.id.MENU_ITEM).setVisible(false);
For all of your menu items in the method onPrepareOptionsMenu(...). You should end up with something similar to this:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.MENU_ITEM_ONE).setVisible(false);
menu.findItem(R.id.MENU_ITEM_TWO).setVisible(false);
menu.findItem(R.id.MENU_ITEM_THREE).setVisible(false);
}
I am a newbie to android when I am going through a sample code for navigation drawer I found he called the method invalidateOptionsMenu() so I searched regarding its functionality but couldn't find an answer so can anyone please brief me its functionality and whenshould we use that.
This function tell android that it should redraw the menu. By default, once the menu is created, it won't be redrawn every frame (since that would be useless to redraw the same menu over and over again).
You should call this function when you changed something in the option menu (added an element, deleted an element or changed a text). This way android will know that it's time te redraw the menu and your change will appear.
Hope this answers your question
I use this method in combination with actionbar: When I need to populate actionbar with new menu items, I call invalidateOptionsMenu(), then onCreateOptionsMenu is called and I can inflate menu that I need. :-)
for more info see http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()
or
Change options menu during runtime - invalidateOptionsMenu()
That would trigger another call to onCreateOptionsMenu where you can decide to display a new menu. It's basically the right way of replacing the current menu with a new one.
When Activity is created then the onCreateOptionsMenu method is called. Inside you can inject menu from menu.xml ol build it by hand. But if you want to change this menu during activity life you must call invalidateOptionsMenu();
eg:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (something) {
// buildOneMenu
} else {
// buildAnotherMenu
}
}
...
something = true;
invalidateOptionsMenu();
invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu(), so that the system could redraw it on UI. This method is a signal for OS to call onPrepareOptionsMenu(), where you implement necessary menu manipulations. Furthermore, OnCreateOptionsMenu() is called only once during activity (fragment) creation, thus runtime menu changes cannot be handled by this method.
Before I ask the question let me give some additional info:
I have a Fragment which adds a MenuItem in the onCreateOptionsMenu. The added MenuItem also has a custom ActionProvider added to it.
The custom ActionProvider doesn't use a ActionView but instead prepares a SubMenu with certain items to choose from. For this reason I've setup the ActionProvider as follows:
I return null in the onCreateActionView method
hasSubMenu() returns true
In onPrepareSubMenu(SubMenu Menu) I first clear the current menu, afterwards add the needed items
I correctly handle the onMenuItemClick in the ActionProvider
Since I'm using ActionBarSherlock, my ActionProvider extends com.actionbarsherlock.view.ActionProvider rather then android.support.v4.view.ActionProvider (don't know if it should make any difference, but at this point I don't know what does)
This all goes well on devices with Android versions higher then 3.0. I see the added MenuItem, it has the correct SubMenu (from the bound ActionProvider) and the correct actions take place for each menu option. But with devices running Android versions below 3.0 (I could only test this on a device running 2.3.6) something weird happens; hence the following question.
Google clearly states:
"onPerformDefaultAction()
The system calls this when the menu item is selected from the action overflow and the action provider should perform a default action for the menu item.
However, if your action provider provides a submenu, through the onPrepareSubMenu() callback, then the submenu appears even when the action provider is placed in the action overflow. Thus, onPerformDefaultAction() is never called when there is a submenu."
Taken from: http://developer.android.com/guide/topics/ui/actionbar.html#CreatingActionProvider
From the excerpt I take it onPerformDefaultAction() should NEVER be called in my custom ActionProvider. Yet on devices running Android version 2.3.6 the onPerformDefaultAction() DOES get called, which also prevents the SubMenu from showing.
My question is; why does the onPerformDefaultAction() gets called instead of the onPrepareSubMenu(SubMenu Menu)? I need a submenu on devices running Android 2.3.6 as well..
EDIT:
I managed to fix my problem using the same technique from the SubMenus.java from the ActionbarSherlock demo-code. This involves adding a SubMenu instead of a custom ActionProvider to the menu in onCreateOptionsMenu(Menu menu), and attaching OnMenuItemClickListener to each MenuItem there.
The workaround is nice and simple. Still, this does not answer my question as to why custom ActionProviders do not work.
In my application i want to add toggle button right side of the Application-name (ie)Right side of BluetoothTextMessaging
Please help me thanks...
May this help you:
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.addSubMenu(0, 1, 1, " Button ").setIcon(R.drawable.file_icon)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
return true;
}
And you can access that button using following code & write logic what to perform on click of that button inside the following code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: {
// Your Logic
break;
}
return super.onOptionsItemSelected(item);
}
I believe that what you're searching for is called an options menu. On Android 3.0+ it is supported. This is how Google's own tutorial describes it:
If you've developed your application for Android 2.3.x (API level 10)
or lower, the contents of your options menu appear at the bottom of
the screen when the user presses the Menu button, as shown in figure
When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six
items, Android places the sixth item and the rest into the overflow
menu, which the user can open by selecting More.
If you've developed your application for Android 3.0 (API level 11)
and higher, items from the options menu are available in the action
bar. By default, the system places all items in the action overflow,
which the user can reveal with the action overflow icon on the right
side of the action bar (or by pressing the device Menu button, if
available). To enable quick access to important actions, you can
promote a few items to appear in the action bar by adding
android:showAsAction="ifRoom" to the corresponding elements
Follow this link: (Menu tutorial) to get to the tutorial.
I can provide code snippets, however the ones on the website are much better :-)