Saturday, July 28, 2012

Hiding Option Menu



In some situation, you developed an app with optional menu but wants temporary disable it for reason. You may use .setVisible(false).

Before move on to an example, I expect you are familiar with Android Option Menu, if not, please read this this first.

I have prepared an option menu defined below

	

    
      
          android:icon="@drawable/ic_import"
          android:title="@string/importSD" />
      
      
            android:icon="@drawable/ic_export"
            android:title="@string/exportSD" />
      
      
            android:icon="@drawable/ic_exportdropbox"
            android:title="@string/exportDropbox" />
      
      
            android:icon="@drawable/ic_unlinkdropbox"
            android:title="@string/unlinkDropbox" /> 
      
      
            android:icon="@drawable/ic_babybirthday"
            android:title="@string/babyBirthday" /> 
      
      
            android:icon="@drawable/ic_info"
            android:title="@string/info" />
      
      
 
 

Then, when the main java, add the following class and set your desired option menu to disable


	

	boolean isLiteVersion = true;
	@Override
	public boolean onPrepareOptionsMenu (Menu menu) {
		if (isLiteVersion)	{
			menu.findItem(R.id.OM_exportSD).setVisible(false);
			menu.findItem(R.id.OM_importSD).setVisible(false);
			menu.findItem(R.id.OM_exportDropbox).setVisible(false);
			menu.findItem(R.id.OM_unlinkDropbox).setVisible(false);
		}
		return true;
	}

Some of the option menu will then be be hidden.




Google Android Developer : Menu

1 comment: