I want to display one image in the Right most corner of action bar. I want this image to be set at runtime depending on some code checks. Can someone guide me how to go about it?
Thanks
Use this. This code adds a refresh button at top right with the icon called R.drawable.refresh. Will show the icon only if there's enough space, else it will show it on the menu
private Menu myMenu
public void changeMenu (int resource) {
myMenu.getItem(0).setIcon(resource); //here resource is your R.drawable.insertid
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
myMenu = menu;
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
menu.add("Refresh").setIcon(R.drawable.refresh)
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
doStuff();
return false;
}
}).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
Just call changeMenu wherever you want to change the code and pass the drawable id.
Edit: Paste this on your activity and add the imports that ask you. You should add your own drawable according to what you want. Remember that icons measure
mdpi: 32x32
hdpi: 48x48
xhdpi: 64x64
if you want to change it at run time you need to make the menu (in onCreateOptionsMenu) and save the menu item. Like this
MenuItem changeableMenuItem = menu.add("new Item");
now you can call changeableMenuItem.setIcon to change the icon, then if you make changeableMenuItem a field in the class you can change the Icon anywhere in the class.
you can reference Dante's answer if you're not sure how to make the menu item.
Related
How can I get an icon from the toolbar to be changed to a new one that I get with a method that is seen in a bbdd.
The problem is that I can not access the event that updates the activity to be able to change the icon.
I tried with the onPrepareOptionsMenu method, but I can not make it work.
I have not been able to do this by putting the code in onStart because it tells me that the menu object is empty or invalid.
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
Drawable iconoMenu = obtenerIconoMenuCarro();
getMenuInflater().inflate(R.menu.menu_categorias, menu);
menu.getItem(0).setIcon(iconoMenu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_categorias, menu);
Drawable iconoMenu = obtenerIconoMenuCarro();
menu.getItem(0).setIcon(iconoMenu);
return true;
}
My activities are extended by AppCompactActivity and loaded through an AdapterView.
And I have the problem when I go back to the fragmentDialog or since the next activity.
Thanks.
For me the simplest way was to keep a reference to the MenuItem for later use.
Obtain when the menu item is inflated.
MenuItem menuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
//find the menu item from the id
menuItem = menu.findItem(R.id.myMenuItem);
return true;
}
Then change the image where you need to with mipmap resource or #drawable.
menuItem.setIcon(R.mipmap.ic_other_icon);
The regular way to change programmatically menu item icon is save reference to the menu after onCreateOptionsMenu() called:
private Menu mOptionsMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
mOptionsMenu = menu;
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
and then access it like: mOptionsMenu.findItem(R.id.action_something).setIcon(R.mipmap.new_icon);
My question is how to set new icon before onCreateOptionsMenu() called - so I don't have reference to the menu?
Thanks,
I don't think there is a way to get the reference to the menu before onCreateOptionsMenu(). Also why would you want to set an icon before the onCreateOptionsMenu?
Apologies for posting it as an answer as I need 50 points to be able to comment.
I'm tring to change a menuitem icon, but the icon is not being changed.
Here is how I find the MenuItem (works fine):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_location_actions, menu);
mActionLocation = menu.findItem(R.id.action_location);
return super.onCreateOptionsMenu(menu);
}
Method UpdateIcon: I took a screenshot so you can see (in the red frame) that the images have been found by the system.
The mActionLocation is a MenuItem which is initialized before this Method is called and is not null.
Anyone has an idea?
UPDATE (solution by help from #vinitius)
The UpdateIcon method:
private void UpdateIcon(boolean locationOn) {
mActionLocation = locationOn; // mActionLocation is a global boolean
invalidateOptionsMenu();
}
The onPrepareOptionsMenu override:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Toggle location icon
if (mActionLocation) {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_on_white_24dp);
} else {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_off_white_24dp);
}
return super.onPrepareOptionsMenu(menu);
}
You need to use onPrepareOptionsMenu(Menu menu):
This is called right before the menu is shown, every time it is shown.You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
Inside this method, retrieve your item as you do in onCreateOtionsMenu and check whether your gps in os or not to modify your item icon.
I want to create my action menu items in the ActionBar totally dinamically for some reasons. But when I add the menu items from code, they are displayed as overflow of the setting menu item.
Below there is my code. any solution?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.start, menu);
MenuItem logoutMI= menu.add(0,1,0,"Logout");
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem configMI= menu.add(0,2,1,"Configuration");
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
I think you need to OR those flag values together on setShowAsAction.
From the docs, http://developer.android.com/reference/android/view/MenuItem.html#setShowAsAction(int)
One of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or
SHOW_AS_ACTION_NEVER should be used, and you may optionally OR the
value with SHOW_AS_ACTION_WITH_TEXT. SHOW_AS_ACTION_WITH_TEXT
Ex.
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Let me know if this actually fixed your problem.
Take a look at the order field of your other menu items, you are adding "Logout" and "Configuration" with an order of 0, but if all your other menu items have an order of 0, they will be ordered based on when they were added to the menu.
Also, you will want to call setShowAsAction() only once, with an or operator:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.start, menu);
MenuItem logoutMI= menu.add(0,1,0,"Logout");
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem configMI= menu.add(0,2,0,"Configuration");
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
I have an ActionBarSherlock with one menu item in my bar. When I'm call
View item = findViewById(R.id.my_item);in activity's button onClick all works fine as expected.
But when I try to do this in onCreate or onResume or even in onPostResume it is always null. I also tryed do this in onCreateOptionsMenu(Menu menu) after inflating my menu from resource, but without any succes.
Therefore I can't understand when actionbar items created and how to catch this moment?
As it has been said here and here, getActionView returns the view that we sets in setActionView. Therefore, the only one way to customize action bar menu item described here
actually it is possible to get the view of the action item, even if it's not customized.
however, do note that sometimes action items get to be inside the overflow menu so you might get a null instead.
so, how can you do it?
here's a sample code:
public boolean onCreateOptionsMenu(final Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
new Handler().post(new Runnable() {
#Override
public void run() {
final View syncItemView = findViewById(R.id.action_search);
...
this was tested when using actionBarSherlock library, on android 4.1.2 and android 2.3.5 .
another alternative is to use a more extensive way , used on the showcaseView library, here .
first of all get a menu reference as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
customMenu = menu;
return super.onCreateOptionsMenu(menu);
}
after that you can get the item you need as below
customMenu.getItem(0);