Question

I've recently started using Android Studio as opposed to the ADT plugin for Eclipse. On the whole I have had very few issues with Android Studio and the transition hasn't been too difficult. This evening I started a new project in Android Studio and quickly setup a basic layout for the main activity and added an action bar with three items in it. The preview of the menu when editing the menu xml file shows the buttons in the action bar, but when I launch the app either on my Galaxy SII (running android 4.2.2) or in the Genymotion emulator (running android 4.3) the buttons are always forced into the overflow menu. I disabled the app title in the action bar to ensure that there was enough room for the buttons but still no luck.

I then created a project in Eclipse and copied over the XML for the menu. On both my phone and the emulator the buttons appear in the action bar instead of the overflow menu. At this point I couldn't see any reason as to why the project should behave differently across the two different IDEs. I imported the Eclipse project into Android Studio and surprisingly when I launched the app, again on the phone and the emulator, the buttons appeared in the action bar.

Has anyone else come across a problem similar to this before? It was quite a frustrating few hours going around in circles until I managed to get a project in Android Studio that worked as I wanted it to.

The following steps in Android Studio 0.5.7 should get you to the same point that I did.

  1. Create a new project.
  2. Set the minimum API level to 11.
  3. Set the target API to 19.
  4. Leave all other settings at their default and create the project.
  5. In the res/menu folder create a file call test_menu.xml
  6. Add the following to the file:

    <item android:id="@+id/testmeniitem"
      android:title="Test Menu Item"
      android:showAsAction="always"
      android:icon="@drawable/ic_launcher"
      />
    

The preview should look like the following: Menu preview

  1. In MainActivity.java update onCreateOptionsMenu so that it is inflating the test menu.

    getMenuInflater().inflate(R.menu.test_menu, menu);

  2. Run the project on your device of choice. The menu item should be present but only in the overflow menu.

If anymore information is required let me know and I will update the question.

Était-ce utile?

La solution

Try as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/testmeniitem"
        android:title="Test Menu Item"
        android:showAsAction="always"
        app:showAsAction="always"
        android:icon="@drawable/ic_launcher"
        />

</menu>

You need to put a custom namespace and set showAsAction twice.

I know this is weird, but it has to be done that way to make sure the showAsAction works on all APIs and with all compatibility libraries.

From the ActionBar buttons documentation page:

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top