-3

I write an app where the user has credit points.

After some action the credit points goes up or down.

I have thought to show the sum of points in the ActionBar.

How would I be able to change this "credit" item text dynamically?

I have seen this post but I only want to change the text. It's not clickable anyhow.

Community
  • 1
  • 1
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

Override the onPrepareOptionsMenu method to include the code which sets the title of the actionBar menu item.

onPrepareOptionsMenu : 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.

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {

    MenuItem menuItem = menu.findItem(R.id.itemID);
    menuItem.setTitle(Integer.toString(scoreVal));
    return true;
}

Now, whenever you want to update the score text, update the instance variable scoreVal and call invalidateOptionsMenu() from the UI Thread. Calling this method will redraw the Menu hence calling onPrepareOptionsMenu() as defined above.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34