Java – Action buttons do not appear in the ActionBar

Action buttons do not appear in the ActionBar… here is a solution to the problem.

Action buttons do not appear in the ActionBar

I’ve been trying to follow official Android development tutorial It’s been a few days, but I’ve noticed some differences between the two IDEs available, Eclipse with ADT plugins and Android Studio. I’ve heard the latter is fine, so this is the one I’m using, but when you first create a new project, the way the file is laid out seems to be different. I was basically able to fix things here and there, so it worked just like on Eclipse (the IDE used in the tutorial), but I couldn’t figure out why my action buttons weren’t showing up in the ActionBar. Here’s tutorial for reference.

So, this is what my res/menu/main.xml file looks like:

<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="com.example.myapplicationn.app.MainActivity" >

<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      app:showAsAction="ifRoom" />

<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:orderInCategory="100"
      app:showAsAction="never" />

</menu>

All I did was replace the action_settings and copy the action_search directly from the tutorial’s sample code. There is a subtle (maybe not so subtle) difference between AS and tutorial: the tutorial file should be res/menu/main_activity_actions.xml but its AS equivalent seems to be the default file res/menu/main.xml, so I agreed. Also, the tutorial example only has the first attribute (xmlns:android="http://schemas.android.com/apk/res/android"), but the file created by AS has these additional attributes those, so I haven’t touched them. I then modified the onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
     Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

Again, the original code used R.menu.main_activity_actions, so I changed it to R.menu.main. The project compiles just fine, a few seconds later MainActivity appears in my phone, but action_search is still hidden in Action overflow (isn’t that called?). ) button. Am I doing something wrong? Any help appreciated. English is not my first language and my writing style is a bit bad, sorry for that.

Solution

I had the same issue recently.

The showAsAction property is located in the app namespace. The tutorial explains that it is used when using compatible libraries. However, if your target API level is high enough, you don’t need to.

I was able to use the android namespace and it worked fine for me. So, change app:showAsAction to android:showAsAction and it will show up where you want it.

Related Problems and Solutions