1

I'm following the this Android tutorial. However, I couldn't place the action button on the action bar. It is always hidden in the overflow. I looked up some related questions Action buttons doesn't show up on Action Bar? as well as this one: Actionbar not shown with AppCompat

But none of them worked for me.

My res/menu/main.xml looks like this:

 <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.gettingstarted.MainActivity" >

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

 <item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"/>

</menu>

And the src/MainActivity.java is as follows:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

I created the folder drawable under /res and added the ic_action_search.png in that folder. Below is the what it looks like when I run the app.

Hidden in overflow

The Search is still hidden in the overflow. Any ideas?

Community
  • 1
  • 1
whileone
  • 2,495
  • 3
  • 21
  • 30

1 Answers1

1

Highest Android API require:

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

<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • Thanks, it worked out. But what is that app:showAsAction? Why it is needed? Could you explain a bit? Thanks! – whileone Mar 19 '14 at 15:27
  • I believe this applies to Android > 4.4 and/or to AppCompat library. Not sure. – shkschneider Mar 19 '14 at 15:38
  • 1
    It is explained on this page: https://developer.android.com/training/basics/actionbar/adding-buttons.html "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." Read on for an example. – Synesso Jan 11 '15 at 11:39