Change icons in ActionBar dynamically

I have an Activity that has an ActionBar , but I need to dynamically change the icons to an ActionBar , I have a pause and a play button, and I need to replace the play button with the pause button as the user clicks on it. I searched and I found it:

 @Override public boolean onCreateOptionsMenu(Menu menu) { if(DEBUG) Log.i("onCreateOptionsMenu()", "onCreateOptionsMenu() -> LogicAnalizerView"); //menu.add("").setIcon(R.drawable.pause).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbarlogic, menu); menu.removeItem(R.id.pauseLogic); return true; } 

Thus, I delete the Pause button, and I can also add it, but this only happens when the Activity and ActionBar launched. How can I make the ActionBar redraw? Also, in this way, the entire ActionBar redrawn. It is right? Is there a way to redraw just the button / icon I want?

Thank:)

+49
android android-actionbar
Jun 13 2018-12-12T00: 00Z
source share
5 answers

You will need to save the link to MenuItem after inflation. So, something like the following:

 public boolean onCreateOptionsMenu( Menu menu ) { MenuInflater inflater = getMenuInflater(); inflater.inflate( R.menu.actionbarlogic, menu ); playMenu = menu.findItem(R.id.playMenu); updatePlayStatus(); return menu; } public void updatePlayStatus() { if( playService.isConnected() ) { playService.isPlaying() ? playMenu.setIcon(R.drawable.pause) : playMenu.setIcon(R.drawable.play); } } 

You can then link to playMenu at any time. This way you can change the item as they say that your player is finishing the game and should return to the play icon.

+44
Jun 13 2018-12-12T00:
source share

Instead of deleting them, you can simply hide the button that you do not want to display.

For example:

 private boolean isPlaying; MenuItem mPlayMenuItem; MenuItem mPauseMenuItem; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbarlogic, menu); mPlayMenuItem = menu.findItem(R.id.action_play); mPauseMenuItem = menu.findItem(R.id.action_pause); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_play: isPlaying = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { this.invalidateOptionsMenu(); } return true; case R.id.action_pause: isPlaying = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { this.invalidateOptionsMenu(); } return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onPrepareOptionsMenu (Menu menu) { super.onPrepareOptionsMenu(menu); if (isPlaying) { mPlayMenuItem.setVisible(false); // hide play button mPauseMenuItem.setVisible(true); // show the pause button } else if (!isPlaying) { mPlayMenuItem.setVisible(true); // show play button mPauseMenuItem.setVisible(false); // hide the pause button } return true; } 

Just a note:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { this.invalidateOptionsMenu(); } 

You need to update the action bar. After 3.0 devices, the action bar does not automatically update. So, you need to manually tell him to call "OnPrepareOptionsMenu (Menu)" so that he updates the elements by calling "Activity.invalidateOptionsMenu ()".

Hope this helps!

Link: http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)

http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#invalidateOptionsMenu(android.app.Activity)

+15
Jul 01 '15 at 16:43
source share
 private Menu mMenu; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main_activity, menu); // Save the menu reference mMenu = menu; return super.onCreateOptionsMenu(menu); } // For example - Call when you need to change icon private void setActionIcon(boolean showWithBadge) { MenuItem item = mMenu.findItem(R.id.ITEM_ID); if(mMenu != null) { if(showWithBadge) { item.setIcon(R.drawable.IC_WITH_BADGE); } else { item.setIcon(R.drawable.IC_WITHOUT_BADGE); } } } 
+9
Feb 12 '15 at 13:33
source share

If you want to get the first item from your menu **

use menu.getItem(0);

this code looks great:

  @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_menu, menu); MenuItem m = menu.getItem(0); m.setIcon(R.drawable.your_icon_here); } return true; } 
+2
Feb 23 '17 at 18:25
source share

Override onPrepareOptionsMenu in your activity class, and then you can add / remove or visible / invisible menu items.

+1
Mar 04 '15 at 5:44
source share



All Articles