1

I have menu file like

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_select_all"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_select_all"/>
<item
    android:id="@+id/action_deselect_all"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_deselect_all"
    android:visible="false"/>
</menu>

and i want to show only one at a time, when i click on one, other show be invisible. can we not get activity's menu by any method of activity.

Raja
  • 161
  • 1
  • 11
  • 1
    Possible duplicate: http://stackoverflow.com/questions/7133141/android-changing-option-menu-items-programmatically – sandkasten May 08 '13 at 11:44
  • set the visibility on menu clicked. – Monty May 08 '13 at 11:45
  • @CobraAjgar but in public boolean onOptionsItemSelected(MenuItem item) {........}, we get the item, so how can set other item's visibility ? – Raja May 08 '13 at 11:49
  • you are making second item invisible in starting...and click on other menu show it..just give it try – Monty May 08 '13 at 11:51

1 Answers1

2

In onCreateOptionsMenu(Menu menu)

after inflate do this

  if (CONDITION) {
            MenuItem item = menu.findItem(R.id.action_select_all);
            item.setVisible(false);
        }
                  else
                        {
            MenuItem item = menu.findItem(R.id.action_deselect_all);
            item.setVisible(false);
        }

Make sure that you call invalidateOptionsMenu(); when you need to refresh menu

Arun C
  • 9,035
  • 2
  • 28
  • 42
  • your method is quite good but that doesn't working because i am showing menu in ActionBar, hence onPrepareOptionsMenu is not called when i click on menu. and also i can't call invalidateOptionsMenu(); as my app's minSdkVirsion=8 – umesh May 08 '13 at 12:10
  • can we check on which android version app is running, in program, then we can make a condition for invalidateOptionsMenu() call – Raja May 08 '13 at 12:15
  • Yes use android.os.Build.VERSION.SDK_INT for that – Arun C May 08 '13 at 12:16
  • wow great, now i can check for have i to call invalidateOptionsMenu() or not, great. thanks a lot – Raja May 08 '13 at 12:21