Context
There seems to be a misunderstanding about this showAsAction
property in the menu configuration. The information I find in the many stackoverflow answers always seem to be incomplete. So here is an attempt to change that.
Three dot | overflow | options menu
There is no way to control if your actionbar will show the three dot overflow menu or not in code.
Well you can force it not to show by having no options menu for sure. And to be fair you can hack around this, more info at How to force use of overflow menu on devices with menu button
The dots showing or not depends on the device you're testing on. Devices with a menu button will NOT SHOW the three dot icon on the actionbar since users can use the menu button on the device.
Devices that don't have this options menu button (like the nexus devices starting from Galaxy) have no alternative to trigger that options menu. Running the same app on these devices will show the three dot overflow menu option in the actionbar.
What you can control
There is the showAsAction
option in the menu xml file that you can use to control how that single menu option will show up in the actionbar.
According to official menu resource doc the options are:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
The ifRoom
flag only shows the option if there is enough room. This should be your first choice in most cases. This will however condense the title in the actionbar (see vertical oriented screenshot).
The never
flag will have this option always in the overflow menu and never directly in the actionbar.
The use of the always
flag is discouraged since it will force the option to always appear in the acitonbar. Even if running on a very small screen size. This could render the title completely invisible.
As you might now an option menu can have both an android:title
and an android:icon
property. You can use the withText
flag together with always
or ifRoom
(use a pipe |
in between) to force the text of the item to show next to the icon when rendered in the actionbar.
The collapseActionView
is only supported from API level 14
so let me ignore that.
A screenshot showing the below code snippet in action on a device hold in portrait mode:
And another screenshot where there is more space in the actionbar thanks to the landscape mode:
Android Documentation
The best development doc about this actionbar would be the actionbar guide. If you're looking more for design guidelines follow this link. The menu resource link was listed before. For menus in general check this.
Code snippet
The code only for reference since that part seems to be right in most answers. A more complete example can be found at https://github.com/AndroidExamples/fragment-navigation
The /res/menu/main.xml
file contents:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="be.hcpl.android.example.MainActivity" >
<!-- an option that never appears as a fixed text/icon in the actionbar -->
<item android:id="@+id/action_one"
android:title="@string/action_one"
android:orderInCategory="100"
app:showAsAction="never" />
<!-- another option that only appears if there is enough room -->
<item android:id="@+id/action_two"
android:title="@string/action_two"
android:orderInCategory="200"
app:showAsAction="ifRoom" />
<!-- a last option that always appears -->
<item android:id="@+id/action_three"
android:title="@string/action_three"
android:orderInCategory="300"
app:showAsAction="always" />
<!-- an option to show with both icon and text -->
<item android:id="@+id/action_four"
android:icon="@android:drawable/ic_menu_directions"
android:title="@string/action_four"
android:orderInCategory="400"
app:showAsAction="withText|always" />
<!-- an option to always show as an icon only -->
<item android:id="@+id/action_five"
android:icon="@android:drawable/ic_menu_info_details"
android:title="@string/action_five"
android:orderInCategory="500"
app:showAsAction="always" />
</menu>
Some linked answers