Android Options Menu is not visible/not called - java

I am using Google My Tracks code from here in my app.
Although in the app itselfthe menu bar is visible. when I integrate it in my app I cannot see the menu bar.
The code is exactly the same.
In the following code I cannot see the system.out.println message.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("Menu created");
getMenuInflater().inflate(R.menu.track_list, menu);
menu.findItem(R.id.track_list_feedback)
.setVisible(ApiAdapterFactory.getApiAdapter().isGoogleFeedbackAvailable());
searchMenuItem = menu.findItem(R.id.track_list_search);
ApiAdapterFactory.getApiAdapter().configureSearchWidget(this, searchMenuItem, trackController);
startGpsMenuItem = menu.findItem(R.id.track_list_start_gps);
refreshMenuItem = menu.findItem(R.id.track_list_refresh);
exportAllMenuItem = menu.findItem(R.id.track_list_export_all);
importAllMenuItem = menu.findItem(R.id.track_list_import_all);
deleteAllMenuItem = menu.findItem(R.id.track_list_delete_all);
return true;
}
If it makes any difference, in all my other activities except the ones imported from this project, I do not use the menu bar.

If this code resides inside a Fragment, make sure you call setHasOptionsMenu(true) in onCreate() method of this Fragment.

Related

How can i change Menu Item icon programmatically?

I need to change my menu icon based on a condition, however when I use Menu.getItem(index).setIcon() it doesn't change my icon at all. I need help how to do that please
Menu.getItem(index).setIcon()
if (menu != null) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
Your code looks correct, but maybe you are using it just once inside onCreateOptionsMenu() and missing the calls that are necessary to update the icon as observedCount updates.
Approach 1
You can try doing the following in your activity:
First, override onPrepareOptionsMenu() and apply the changes there:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (observedCount != 0)
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
menu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
return super.onPrepareOptionsMenu(menu);
}
Then, whenever you want the icon to be updated (i.e., whenever observedCount changes), just invoke:
invalidateOptionsMenu(); // From a fragment, call activity.invalidateOptionsMenu();
Approach 2
Another option is to save a reference to the menu inside onCreateOptionsMenu():
private Menu mMenu;
...
#Override
public void onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenu = menu;
}
Then, you can use that reference to set the icon:
if (observedCount != 0)
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_delete_copy));
else
mMenu.getItem(2).setIcon(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_cart));
That should hopefully do it!
P.S. Make sure you are getting the correct item when doing menu.getItem(2) (the item number 2 is actually the third one because the count starts at 0). To avoid getting the wrong item, it would be a good idea to set the item you want to change with an ID and then replace menu.getItem(2) with menu.findItem(R.id.your_item_id).

How to add animation for showing and hiding contextual action mode?

I am wondering how to animate going into and out of the contextual action mode. I am looking for an animation like in the app AwSms when you long press on a conversation (image provided, sorry for no video) The animation is essentially a ripple that comes from the middle and expands outwards on the Action Bar. I have searched long and far with no foreseeable way to animate the contextual action mode. It was definitely done here but I am not sure how. Thank you very much in advance!
Things I have tried:
public ActionMode.Callback iconEditCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_iconmanager_edit, menu);
mode.getCustomView().startAnimation(ripple);
return true;
}
I also tried:
getSupportActionBar.setShowHideAnimationEnabled(true);

Menuitems in my Fragments do not change according to the code for the first time I access Activity

I am using ActionBarActivity with Fragments.
I have already changed the implementation of how menu items must show in actionbar too many times. For example, changing the local of onCreateOptionsMenu to Activity and Fragments.
The first fragment must not show the menu items and another fragment must show one more menu item. Others fragments show the menuitems normally.
The problem: when I enter in the activity for the first time the menu items don't show in any fragment. If I rotate the device everything works fine.
In my others attempts, the behaviour change to: when I enter in the activity for the first time the menu items show in all fragments (the first one has setHasOptionsMenu(false);). And the fragment that have one more menuitem don't show it.
Sorry for bad English.
Working code (after rotate the device):
FirstFragment:
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
int count = menu.size();
for (int i = 0; i < count; i++) {
menu.getItem(i).setVisible(false);
}
}
MainActivity:
public boolean onCreateOptionsMenu(Menu menu) {
for (int i = 0; i < itens.size(); i++) {
item = (Item) itens.get(i);
MenuItem mMenuItem = menu.add(0, i, i, item.getLabel()).setIcon(item.getImage());
MenuItemCompat.setShowAsAction(mMenuItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
}
return super.onCreateOptionsMenu(menu);
}
I used it in the constructor:
setHasOptionsMenu(false);
but it didn't work. Then I used it in other method called in the constructor toghether with codes to reflesh the fragment and it works now.

Is there a way to change the text of an Action Bar Menu Item when I touch the menu item in Android?

When I touch this "Edit" button I want it to change to "Done". Is there a way to do this?
I tried this but it only works when opening the menu on the right side:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// change the text here.
return super.onPrepareOptionsMenu(menu);
}
I assume that, at some point, you inflate the menu, yes?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.some_menu_layout, menu);
return true;
}
So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)

Get options menu to show in system bar

I'm trying to get my options menu to show up in the new Android 3.0 system bar. I can get this behavior to work with the Notepadv3 tutorial. However, when implementing nearly the identical functions, the menu shows in the above action bar, but not the system bar. Any ideas?
Attached is the appropriate code for my application:
public class AACUser extends Activity {
private static final int ADMIN_ID = Menu.FIRST;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADMIN_ID, 0, R.string.menu_admin);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case ADMIN_ID:
enterAdminMode();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
}
I've also tried implementing these menu functions as recomended by the creating menu documentation:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_adminMode:
enterAdminMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
but no difference: it still displays the option in the overflow of the action bar, but not the system bar.
As of Android 3.0, the options menu is not required (if not deprecated). If you target API 11 (Android 3.0) or later, it is assumed that your app will work without an options menu, so the system bar will not display one. The options menu in the system bar is only there for backwards compatibility with old applications. New applications should have another affordance for accessing options, such as that provided by the action bar.
I think that the menu button in the system bar is for backward compatibility purposes only. So if you want to design your app for Honeycomb then the menu should be and will be in the action bar.

Categories