14

I'm trying to display a data count in the top right corner of my Action Bar.

If I wouldn't be using a split action bar I would just create a TextView in the bar, but it's currently not possible to have items in the top Action Bar when you have split.

To clarify, what I mean is this:

Gmail unread count

Did Google use a different Action Bar implementation or is there a way to do this? I think I remember reading about it, but I can't find how it was called.

Community
  • 1
  • 1
Tim van Dalen
  • 1,470
  • 3
  • 21
  • 41

2 Answers2

25

Use a custom view with your spinner and text view for the top AB and enable the android:uiOptions="splitActionBarWhenNarrow" in the manifest declaration for your activity

//set the actionbar to use the custom view (can also be done with a style)
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

//set the custom view to use
getActionBar().setCustomView(R.layout.custom_ab);

Where custom_ab.xml is a layout with your ImageView, Spinner and TextView for the count.

roflharrison
  • 2,300
  • 23
  • 26
  • 2
    The custom view of the ActionBar is supplemental to the bar itself, which is not entirely clear from the documentation. As such, this answer is basically all you need, though you forgot to mention that then you would call getActionBar().getCustomView() to retrieve the view when needed. – iHearGeoff Dec 04 '12 at 03:59
  • 8
    Also, if you want to keep the app icon + up affordance: `getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM);` – elyas-bhy Mar 11 '13 at 13:26
  • you won't need to create a spinner manually. you can still use the navigation list mode. just set `getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);` after the `setCustomView()` method call. – rogcg Oct 06 '13 at 18:40
0

You should stop using ActionBar or like and update your app to use Toolbar which has a simple and much easy interface to do the everything you can think of. For reading about it use the [Android Blog][1]

[1]: http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html for the pre lolipop apps.

Uzair
  • 1,529
  • 14
  • 17