Monday, 15 March 2010

Android: Can't toggle MenuItem icon -


I am trying to change a menu icon, but the icon is not being changed.

Here's how I get MenuItem (works fine):

  @Override Public Boolean onCreateOptionsMenu (menu menu) {// Action Bar MenuInflater Inflight = getMenuInflater ( ) To increase the menu item for use; Inflater.inflate (R. menu. activity_pace_reactions, menu); MActionLocation = menu.findItem (R.id.action_location); Return Super. Secret Option Menu (menu); }  

Method UpdateIcon: I have taken a screenshot so that you can see (in red frame) the images have been detected by the system.

Enter image details here

mActionLocation < / Code> is


UPDATE ( @ Solution with help from Vinitius )

UpdateIcon method:

  Private Zero UpdateIcon (Boolean LocationOn) {mActionLocation = locationOn; // mActionLocation is a global boolean invalid IptionsMenu (); }  

onPrepareOptionsMenu override:

  @Override Public Boolean customization options menu (menu menu) {// toggle location icon if (mActionLocation) {menu.findItem ( R.id.action_location) .setIcon (R.drawable.ic_location_on_white_24dp); } And {menu.findItem (R.id.action_location) .setIcon (R.drawable.ic_location_off_white_24dp); } Return super. Paraphrase option menu (menu); }  

You need to use:

This is shown every time before the menu is displayed. You can efficiently enable / deactivate objects using this method or otherwise modify the content dynamically.

Within this method, retrieve the item as you do in onCreateOtionsMenu and check whether your GPS is in the OS or your item icon Modify


No comments:

Post a Comment