I have an application that has an options menu in fragments that works perfectly in 4.0. But when i test it in 2.3 i get a weird issues. When i click on an option menu it doesn't cause any events to trigger. But if i do anything else after like click back, click a button, open another activity. The event that was supposed to happen when i click the action button triggers.
I have setHasOptionsMenu(true); in my fragments the only thing different that maybe the source of the issue is that my fragments inherit from a base fragment that has the logic for the action buttons.
---Base Class ---
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if(menu != null)menu.clear();
inflater.inflate(R.menu.msg_menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
//Logic Here
}
}
To make this issue more strange sometimes the button in the actionbar will behave normally but i can't figure out why.
EDIT: I found that if i press a menu button then open and shut the navigation drawer. The button then work correctly after but still haven't found a work around
Related
I am having Viewpager with 3 fragments. I want to show menu in only one of the fragments.
1st, I don't know why toolbar.inflateMenu doesn't work.
2nd, the menu works, if I have onPrepareOptionsMenu method and do
getMenuInflater().inflate(R.menu.add_user, menu); but the menu is displayed in all the fragments.
So, according to other questions in stack overflow, I implemented the below code, but it doesn't show the menu, it enters the method but menu is not shown.
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
//getMenuInflater().inflate(R.menu.add_user, menu);
info("Menu=>${tabs_viewpager.currentItem}")
val menuItem = menu?.findItem(R.id.addUserMenu)
menuItem?.setVisible(tabs_viewpager.currentItem == 1)
return true
}
The control comes to this method and shows currentItem. But it doesn't display the menu. Menu is there with id. Can someone direct me what can I correct to get this work?
try calling setHasOptionsMenu(true); in oncreate() of the fragments where you want the menu to show (you can set it to false in the fragments where you do not want the menu to show).
also include:
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// your code
}
in the fragments where you want to set/change the menu. this is called everytime before the menu is shown.
you can also call invalidateOptionsMenu() or supportInvalidateOptionsMenu() (if you're using the support library) to force onPrepareOptionsMenu(menu) to be called.
you may want to check out this menus tutorial
good luck
clive
I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.
This topic can look similar to others but I haven't found any usable answer for this case.
Here is what I want : I have a TabActivity with a menu, containing tabs without any menu. When I press the menu button, I want the only existing menu to be displayed. If I only inflate the menu, this is working fine. But if I want to change the content of the menu (change the visibility of items in the onPrepareOptionsMenu(menu) method) or even press the items of the menu, nothing works.
For the onPrepareOptionsMenu(menu), the problem seems to be coming from the menu.findItem(...) method which returns a null Object, and I have a NullPointerException !
In onOptionsItemSelected(item), none of the item IDs will be recognized.
I have noticed with the debugger that the menu Context is the activity of the current tab, so I have moved the menu inside of that activity instead, but without more success.
Last thing, I was using the same menu and very similar code in a previous version of the app, using a single activity (no tabs) and I didn't encounter any problem. When I moved to the TabActivity design it was first working fine (maybe the context of the menu was my TabActivity instead of the Activity of the tab), but it didn't work anymore after minor changes to the Activities of the tabs (nothing related to any menu).
If you think using ActionBar (and the support package) would fix this, I am already planning in moving to it later, but I would like to understand and fix this first.
Here is the code :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem connect1 = menu.findItem(R.id.connect_1);
MenuItem connect2 = menu.findItem(R.id.connect_2);
// Here I do some stuff to prepare the menu, which could be simplified
// like this :
if (device1Connected)
if (!connect1.isVisible()) // here I get the NullPointerException
connect1.setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case R.id.select_device:
// do some stuff
return true;
case R.id.connect_1:
// do some stuff
return true;
case R.id.connect_2:
// do some stuff
return true;
case R.id.disconnect:
// do some stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is the XML file of the menu, nothing special
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_device"
android:icon="#drawable/icon"
android:title="#string/choose_devices"/>
<item android:id="#+id/connect_1"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_1" />
<item android:id="#+id/connect_2"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_2" />
<item android:id="#+id/disconnect"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="#string/disconnect" />
</menu>
Edit : I have added the following in the onPrepareOptionsMenu(menu) to avoid the NullPointerException. It doesn't fix the real problem but I can now see precisely what happens when I click on a MenuItem
if (connect1 == null || connect2 == null)
return super.onPrepareOptionsMenu(menu);
When I click on the first item, getItemId() returns, for example, 2131165207, and the second item returns 2131165208 (I have checked, they are ids of Views of the second tab !!), but the values it should return to enter the switch/case are respectively 2131165215 and 2131165216, so as I said before, I have a problem with my item ids. I made this test with the menu within the Activity of the first tab, because the mContext value of the menu is always an instance of the current Activity. But even though the Context AND the Activity containing the menu are the same, it still doesn't work.
So ! I have finally figured out what was wrong. It was a building/compiling error. I have been trying before refreshing the project many time, but it was not enough, I had also tried deleting the R.java file (where are compiling the ID numbers from the XML files), but it still didn't work; and I finally remembered about the Project -> Clear... function in Eclipse while working on another project.
And it just worked. So that was nothing there very challenging, but at least I've learnt something valuable today.
I am using tabs, with Activity groups in each. I want to add an options menu to certain activities but it won't show on any. I have options menus working in other projects with the exact same code, so I can't figure out why they won't show up. This is the code I am using:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Option 1");
menu.add("Option 2");
menu.add("Option 3");
return super.onCreateOptionsMenu(menu);
}
Is there anything wrong with this code or anywhere else I should be looking at that might be blocking this menu from showing when I hit the menu button?
The only thing that would be blocking the menu AFAIK would be if you are overriding onKeyDown. Make sure you aren't overriding that method and thus preventing the menu button from doing what it is supposed to.
I have an Options menu up and running in my Android application and I've overridden the onCreateOptionsMenu, onOptionsItemSelected and onPrepareOptionsMenu methods to customize the menu a little.
My question is related to keeping the Options menu open after the user clicks on a menu item. Basically, I'd like to be able to hide the menu until the user clicks on the device menu key. Once the user clicks on this key, I'd like to be able to hold the menu in place regardless of how many times the user clicks on menu items. If the user wants to hide the Options menu, they'd just need to click on the device menu key again.
Is this type of interaction supported (or even advisable). If this interaction is not supported, any alternative suggestions are more than welcome.
Cheers!
Sean
This will not be possible with onCreateOptionsMenu and the other methods. They always act that way.
But you can do it another way. But there you have to program the whole menu yourself. Basically add the Menu in your layout.xml and let it be hidden (visibility = gone). Then you overwrite the methods onKeyDown. There you check if it is the Menu key. if the menu is not yet open yes, then you show the menu. If it is open already, you hide it.
should not be too difficult. Good thing as well is, that you can make the menu look exactly the way you want and as well let it react the way you want.
For anybody like me, who found this question in google:
To keep menu open after selecting item, you need this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(!item.isChecked());
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
Important to return false in onOptionsItemSelected and methods of OnActionExpandListener
This solution from #MayurGajra. More details here: How to hold the overflow menu after I click it?