3

I'm following the google Android Studio first android app tutorial. But I get 3 weird errors now when trying to add a search bar to my app.

I'm here right now and I added the XML code just like the tutorial.

http://developer.android.com/training/basics/actionbar/adding-buttons.html

The errors I get:

Error:(5, 23) No resource found that matches the given name (at 'icon' with value '@drawable/ic_action_search').
Error:(6, 24) No resource found that matches the given name (at 'title' with value '@string/action_search').

At android:showAsAction="ifRoom" I'm getting the weird error:

Excecution failed for task ':app:procesDebugResources'.

This is my XML code:

<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=".MainActivity">
    <item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        android:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

Whats wrong in this code?

Thanks for reading/helping!

Stefan
  • 1,905
  • 7
  • 24
  • 38

1 Answers1

5

Try 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=".MainActivity">
    <item android:id="@+id/action_search"
          android:icon="@android:drawable/ic_menu_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>

A few things to note:

1) You should change android:showAsAction tag to app:showAsAction, this is to do with compatibility with older versions of android.

2) I've changed your search icon to one embedded in Android. What you're doing is trying to use a native Android icon. Seems like this has changed since your tutorial. You can always use your own icon and put it in the res/drawable folders in your project.

3) Just as @drawable is a reference to your drawables, @string is a reference to the strings.xml file in your res/values folder. You need to open this xml file and add something like:

<string name="action_search">Search</string>

Hope that helps, good luck.

James
  • 3,485
  • 3
  • 20
  • 43
  • Thanks that solved those 3 errors but now I'm getting this error in my java code: Cannot resolve symbol "R" this is how the code looks like: if (id == R.id.action_settings) { Why does it end up with this error now this didn't happen before I changed the XML file? – Stefan Jan 06 '15 at 09:55
  • Hmm the Cannot resolve symbol R is a tricky one and could be a wide range of things. First check all your res xml very carefully for any mistakes. Then try and clean and re-build. http://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-android-studio – James Jan 06 '15 at 10:04
  • Allright for some reason just restarting my Android Studio allready solved the issue Thanks! – Stefan Jan 06 '15 at 10:08