9

Getting started with ActionBar...

For MenuItem.SHOW_AS_ACTION_IF_ROOM the documentation says:

Show this item as a button in an Action Bar if the system decides there is room for it.

My question is, how does the system decide if there's room, and can I affect/tweak that decision?

In my case, I have a few menu items that would be appropriate to show as action buttons--if there's enough room. On smaller screens they take enough space to make actionbar's title ellipsized. I'd prefer them to go into action overflow in this case, instead of obscuring title.

PS. I am using ActionBarSherlock and haven't checked if native ActionBar acts exactly the same. If native ActionBar doesn't allow "if-room" action items to shrink title too much, then cool, I'll go looking for a bug in ActionBarSherlock.

3 Answers 3

8

According to the official Android Design guide (http://developer.android.com/design/patterns/actionbar.html):

Action bar capacity is controlled by the following rules:

  • Action buttons in the main action bar may not occupy more than 50% of the bar's width. Action buttons on bottom action bars can use the entire width.
  • The screen width in density-independent pixels (dp) determine the number of items that will fit in the main action bar:
    • smaller than 360 dp = 2 icons
    • 360-499 dp = 3 icons
    • 500-599 dp = 4 icons
    • 600 dp and larger = 5 icons
Sign up to request clarification or add additional context in comments.

Comments

4

OK, this wasn't too hard to figure out, the logic is in ActionMenuPresenter.java

It appears that:

  • action buttons are not allowed to take more than half of the horizontal space
  • the number of displayed action buttons is limited too, based on screen width (R.integer.abs__max_action_buttons)
  • The numeric limit can be broken if actions belong to the same group, width limit cannot--if a group breaks width limit, whole group goes in overflow.

Comments

1

I'm ActionBarSherlock user, i can suggest you to browse Wharton's sources for the answer.

As i understand, he has his on layout with onMeasure method where he checks if the title, logo, and menu item views can be placed in the actionbar, by defining their summary width.

You can check his logic, if i can trust my memory, in BaseMenuPresenter class.

4 Comments

The logic appears to be ActionMenuPresenter class: action items can take no more than half the horizontal space, and max number of actions is limited too, based on screen width. And then there are exceptional cases....
Yes there are, if you place all menu showAsAction flags to always, they will all appear in actionbar, I solved that problem by smart overriding of update menu method in BaseMenuPresenter class.
Check for actionbarsherlock 4.0 beta, i suppose it will solve your problem
@Orest But what if you have too many that some should go to the overflow menu?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.