Since I just created this blog, let’s make a short warm-up post.

Today I encountered one problem when developing an app. As most of us know, on android we can show menus as text directly on action bar or as action overflow (Image below).

Menu

Everything seemed to be fine on nexus device but when we moved application to one of the phones with physical buttons, menu disappeared from action bar completely. It took me a while but I have found the answer on one of the stackoverflow pages:

...the action overflow is available from the menu hardware key. The resulting actions popup... is displayed at the bottom of the screen.

After clicking ‘menu’ button on phone, everything became clear… well it is one the android anti-patterns but what if you want to show the menu on the action bar anyway? Instead of standard xml file like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapplication="http://schemas.android.com/apk/res-auto">

  <item
      android:id="@+id/first_item"
      android:title="@string/first_item_string"
      />

  <item
      android:id="@+id/second_item"
      android:title="@string/second_item_string"
      />

</menu>

just create an item and put the menu into it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapplication="http://schemas.android.com/apk/res-auto">
    <item
	android:id="@+id/menuid"
	android:icon="@drawable/ic_action_overflow"
	android:title="@string/menu_title"
	yourapplication:showAsAction="ifRoom">
	<menu>
	    <item
	        android:id="@+id/first_item"
	        android:title="@string/first_item_string"
	        />

	    <item
	        android:id="@+id/second_item"
	        android:title="@string/second_item_string"
	        />
	</menu>
    </item>
</menu>