Reputation: 42844
What i have::
I have a menu in actionbar it has two sub-items in it
buffet_map_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_cart"
android:icon="@drawable/ic_action_camera"
android:showAsAction="ifRoom"
android:title="cart">
<menu>
<item
android:id="@+id/action_cart_buy"
android:icon="@drawable/ic_action_camera"
android:title="Sort by newest"/>
<item
android:id="@+id/action_cart_reserve"
android:icon="@drawable/button_reserve_selector"
android:title="Sort by rating"/>
</menu>
</item>
</menu>
What i am trying to do::
action_cart_buy
and action_cart_reserve
Upvotes: 1
Views: 340
Reputation: 728
You can use onPrepareOptionsMenu(Menu)
for dynamic updates:
Prepare the Screen's standard options menu to be displayed. 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.
You can use it like:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem buyItem = menu.findItem(R.id.action_cart_buy);
MenuItem reserveItem = menu.findItem(R.id.action_cart_reserve);
buyItem.setTitle("New buyItem String");
reserveItem.setTitle("New reserveItem String");
return super.onPrepareOptionsMenu(menu);
}
Upvotes: 2
Reputation: 5480
You have access to the Menu
object in onCreateOptionsMenu
or onPrepareOptionsMenu
(better).
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_cart_buy);
item.setTitle("Any title");
return super.onPrepareOptionsMenu(menu);
}
To make the UI update your ActionBar at the right time, you have to call:
invalidateOptionsMenu();
Upvotes: 0
Reputation: 1857
Take a look at findItem from Menu and than MenuItem, there is a setTitle.
Edit-- There is a topic called "Changing menu items at runtime" in this article which may help you as well.
Upvotes: 1