How to open OptionsMenu on tablet?

Upvote picture Upvote · Jan 12, 2012 · Viewed 8.1k times · Source

I have developed an android app that mainly targets smartphones. However in tablet emulator I see that it works on android 3.x, too.

However there is one little problem. The user cannot open OptionsMenu when he clicks on the menu button. As you know on the smartphone a menu appears from the bottom. But on tablet nothing happens.

I have read this http://developer.android.com/guide/topics/ui/menus.html#options-menu but still cannot figure out how to manage this. My app has a custom action bar.

My code is quite straight forward. In main activity:

@Override
public boolean onCreateOptionsMenu(Menu men) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.menu, men);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    //...
    }
}

And the prefs activity:

public class MdPrefsActivity extends PreferenceActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         getPreferenceManager().setSharedPreferencesName(
                 MdSharedPrefs.PREFS_NAME);
         addPreferencesFromResource(R.xml.prefs);
     }
}

MdSharedPrefs class just contains some getters and setters to retrieve/write the pref values.

Any ideas how I can show the OptionsMenu on tablet?

Answer

Francesco Vadicamo picture Francesco Vadicamo · Jan 12, 2012

As the documentation said:

Items in the Options Menu are accessible in two distinct ways: the MENU button or in the Action Bar (on devices running Android 3.0 or higher).

[...]

On Android 3.0 and higher, items from the Options Menu is placed in the Action Bar, which appears at the top of the activity in place of the traditional title bar. By default all items from the Options Menu are placed in the overflow menu, which the user can open by touching the menu icon on the right side of the Action Bar. However, you can place select menu items directly in the Action Bar as "action items," for instant access [...]

So for Android 3.0 or higher you can see only the menu items in the ActionBar.

It is also important to notice that:

Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or greater.

But be aware that the ActionBar is visible only if you don't have an application or activity theme that explicitly hides it like

android:theme="@android:style/Theme.Holo.NoActionBar"