12

I have looked through the questions on stack overflow and can't find the solution.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.themenu, menu);
    MenuItem   item = menu.findItem(R.id.menu_settings);
    MenuItem item2 = menu.findItem(R.id.menu_save);
    item.setVisible(isdown);
    item2.setVisible(isdown);
    return true;
}

This sets my menu items to visible (item1 and item2). the onclick works fine

public void inflateTextarea() {
    if(isdown == true) {
        isdown = false;
        LinearLayout tl = (LinearLayout)findViewById(R.id.content);
        tl.setVisibility(View.VISIBLE);
        ScaleAnimation scale =  new ScaleAnimation(1, 1, 0, 1);
        scale.setFillAfter(true);
        scale.setDuration(500);
        tl.startAnimation(scale);
    }
}

Then this sets my isdown boolean to false. on stack people say that the onPrepareOptionsMenu should fire everytime I click but this is not the case. I am able to hide one menu item on the onclick function

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menu_settings: 
            Log.v("Log:","edit_item pressed");
            return true;
    }
}

But I have multiple menu items that I need to hide and others that I want to show. how can I go about this?

4 Answers 4

19

Its not enough to change the isDown variable. You have to call the setVisible() method every time you want to change the visibility. That method does more than just setting a boolean value, so just changing a boolean value will not do.

After changing the isDown value to false, you need to call invalidateOptionsMenu() which will re-launch the menu by calling onPrepareOptionsMenu() again.

Try this code for making the menu items unvisible:

...
isdown = false;
invalidateOptionsMenu();
...
6
  • yes I know that, but as i read the onPrepareOptionsMenu(Menu menu) { is supposed to fire everytime a menu item is clicked and THAT would work. I cant set the visibility unless I have Menu menu in the function, so that is where I am having a problem
    – Dnaso
    Commented Apr 19, 2013 at 23:27
  • I see. Edited post to cover this.
    – tbkn23
    Commented Apr 20, 2013 at 7:14
  • where do i add the invalidateOptionsMenu() method?
    – Dnaso
    Commented Apr 20, 2013 at 20:28
  • Anywhere within the activity after you change the value of isDown to false
    – tbkn23
    Commented Apr 20, 2013 at 20:37
  • nvm found it. i had to add a suppression for new api so i put the invalidation in its own function just incase. thank you
    – Dnaso
    Commented Apr 20, 2013 at 20:38
11

Try this,

private Menu menu=null;
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater)
{
    inflater.inflate(R.menu.note_menu, menu);
    this.menu=menu;
super.onCreateOptionsMenu(menu,inflater);
menu.findItem(R.id.menuChartNoteEdit).setVisible(isdown);
menu.findItem(R.id.menuChartOpenNote).setVisible(isdown);
}

When you want to hide menu at any where, after executing onCreateOptionsMenu() then just change value for isdown and repeat this code,

menu.findItem(R.id.menu_settings).setVisible(isdown);
menu.findItem(R.id.menu_save).setVisible(isdown);
5
  • i heard the onPrepareOptionsMenu was supposed to be better, do you know what the difference is?
    – Dnaso
    Commented Apr 20, 2013 at 4:40
  • ok so now i can put my menu.find item in any function in my class?
    – Dnaso
    Commented Apr 20, 2013 at 4:41
  • yes, find Menu Item in any where on your class and hide/show..!, Are you check that?
    – Sino Raj
    Commented Apr 20, 2013 at 12:13
  • i will try it out if it works will up vote thank you so much give me a few hours
    – Dnaso
    Commented Apr 20, 2013 at 14:34
  • both worked but the other answer by invalidating the menu is more efficient thank you.
    – Dnaso
    Commented Apr 20, 2013 at 20:37
0

Try this in your FragmentActivity

public boolean onPreparePanel(int featureId, View view, Menu menu) {
    if(isLeftMenuOpened()) return false;
    return super.onPreparePanel(featureId, view, menu);
}

Method FragmentActivity.onPreparePanel creates menu for all attached fragments. You must return true for the menu to be displayed; if you return false it will not be shown.

0

I have a menu with two items, first: save item, second: edit item. I wanted when I came in to edit, at first "save item" had been hidden, and when I click "edit item", "save item" was shown.

So:

  1. declare an item in public
  2. set value to it in onCreateOptionsMenu(Menu menu, MenuInflater inflater)
  3. set visibility to isHidden()
  4. then every where I wanted, set visibility to isVisible()

Public region:

MenuItem saveItem;

onCreateOptionsMenu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_detail_products, menu);
    saveItem = (MenuItem) menu.findItem(R.id.action_detail_product_save);
    saveItem.setVisible(isHidden());
}

Where i want:

saveItem.setVisible(isVisible());

Finally it works as I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.