1

I would put a number on the button that represent the numbers of notify like facebooks app. I follow the first example in this topic :

Actionbar notification count icon (badge) like Google has

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuItem item = menu.findItem(R.id.badge);

        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        notifCount = (Button) MenuItemCompat.getActionView(item);

        notifCount.setText(String.valueOf(SingletonGrafica.getNumeroNotifica()));
        return super.onCreateOptionsMenu(menu);
    }

When I call :

MenuItem item = menu.findItem(R.id.badge);

I obtain NullPointer Exception.Anyone can help me?

Community
  • 1
  • 1
Gozilla
  • 13
  • 3
  • Welcome to Stackoverflow, It is a helpful practice to mark an answer as correct if you receive one. (I think you received two) – Raanan Apr 10 '15 at 15:14

2 Answers2

2

You need to create the menu first:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the navigation_menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation_menu, menu);
        return true;
    }

and if you want to update the menu you should do it in: onPrepareOptionsMenu

if you want the onPrepareOptionsMenu function to be called again you can call invalidateOptionsMenu();

Raanan
  • 4,777
  • 27
  • 47
0

You can also add item from code

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem item = menu.add("Badge");MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        return true;
    }
bongo
  • 733
  • 4
  • 12