Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MenuItem.SHOW_AS_ACTION_IF_ROOM work?

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.

like image 369
Pēteris Caune Avatar asked Jan 15 '12 17:01

Pēteris Caune


2 Answers

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
like image 186
M.Q. Avatar answered Nov 04 '22 15:11

M.Q.


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.
like image 26
Pēteris Caune Avatar answered Nov 04 '22 16:11

Pēteris Caune