2

I am new to Android Development. I am following the Beginning Development Tutorial from the developer.android.com. Unfortunately, I'm stuck trying to add a Search Action Item to the Action Bar. This is my XML and the onCreateOptionsMenu that corresponds:

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

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

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

</menu>

.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

My drawable directories contains the ic_action_search.png for all the screen densities. Also, my SDK min is 8 and the target is 19. I have imported the v7.app.ActionBarActivity as well.

The setting and overflow menu show up, yet the Search is still missing. What am I doing wrong?

1 Answer 1

2

It could be because you are using the compatibility library in which case you need to add extra attributes to the menu XML file. This is my item for the search option...

<item android:id="@+id/options_menu_search"
    android:title="@string/search"
    android:icon="@android:drawable/ic_menu_search"
    com.mycompany.myapp:showAsAction="ifRoom|collapseActionView"
    com.mycompany.myapp:actionViewClass="android.support.v7.widget.SearchView" >
</item>

Notice the two lines which start with com.mycompany.myapp - those are necessary when using the compatibility menu options. Change that prefix to match your app's package name.

1
  • 1
    @AlanW : You'll find you do need those extra lines with the package prefix for devices running below API 11. I can't find the documentation link at the moment but when I do I'll post it. There are a number of places you have to do similar things when using the compatibility libraries.
    – Squonk
    Jun 19, 2014 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.