Friday, 23 October 2015

how to control action bar items programmatically

First you should keep menu reference when your activity do "onCreateOptionsMenu()"

Make the menu static or public to access if you want to control your menu from other place. (you can't control your menu from other thread, otherwise you will get error which does not crush your app, but you can see the error on android studio console. in that case you need use Handle, to do so you need check another blog of me

You can modify you menu by this:

MenuItem menuItem = menu.getItem(1);

getItem(index) will use the items order of your menu.xml.

Please don't call "invalidateOptionsMenu();" after your modify. that will make you activity call "onCreateOptionsMenu()" again and recreate all menus.

No comments:

Post a comment