I would like to replace the icon used for the ActionBar element at runtime (it appears as an action element in the ActionBar). I am trying to do the following:
private Map<Integer, Integer> mPendingReplacements = new HashMap<Integer, Integer>(); @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { for (Map.Entry<Integer, Integer> it : mPendingReplacements.entrySet()) { MenuItem item = menu.findItem(it.getKey().intValue()); if (item != null) { item.setIcon(getResources().getDrawable(it.getValue().intValue())); } } mPendingReplacements.clear(); return true; } private void replaceMenuItemIcon(int menuItemId, int drawable) { mPendingReplacements.put(menuItemId, drawable); invalidateOptionsMenu(); } private void onUserClickedSomeButton() { replaceMenuItemIcon(R.id.foo, R.drawable.grok); }
------------ Update -----------
Updated the above mPendingReplacements usage example proposed by Selvin. I see that it runs as excluded, but the action bar icon still doesn't change immediately. They will continue to delve into this, but can anyone find out if I missed something?
thanks
source share