Thursday, August 4, 2016

Programmatically updating the Menu Item drawable icon

If there is a requirement that different drawable icon has to be shown on different activities, then it can be achieved by using the following line of codes -
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.menuItem);
item.setIcon(R.drawable.ic_drawer);
return super.onCreateOptionsMenu(menu);
}
where R.menu.main is the layout used for the Menu inside the "Menu" Folder in "res" folder,
R.id.menuItem is the identifier of the particular menu item inside R.menu.main layout file.

No comments:

Post a Comment