27

I am new to Android Development. I was just following the basic turorial on developer.android.com to create my first application.

As I was creating the Action BAr, I had to add this Search button to it. I created the menu xml and this is what I had written on it.

<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />

Now my question is on this part of the XML

android:icon="@drawable/ic_action_search"

I get a compilation error in eclipse, which says,

error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_action_search').

I checked on developer.android.com and also googled it out, but I'm just not able to locate a solution.

I need to solve this problem. As always, I am able to skip this part of the XML and what I get is just plain text "SEARCH", but the icon will definitely look better.

1
  • Where did you put your ic_action_search?
    – Long Dao
    Aug 7, 2013 at 3:51

8 Answers 8

42

Here is the easy way if you're using Eclipse:

  • Ctrl+N (emacs keybindings) to open the file wizard
    • 'File | New | Image Asset' in Android Studio
  • Select Android, "Android Icon Set"
  • Edit name ic_action_xx, click Next
  • Clipart, choose the icon you want
  • Select theme (Holo Light means a light background, Holo Dark means a dark background)

Eclipse will automatically populate the res/drawable-* directories with the new icon, scaled to the directory size. No need to download anything, understand PNG or copy the icon into multiple directories.

I use the file wizard A LOT.

4
  • How to approach this in android studio?
    – iMom0
    Dec 7, 2013 at 13:06
  • Yeah, I switched to Android Studio and like it a lot. But I can't remember how to create a new file - I've been working on the kernel. I'm sure it's in one of the tool windows.
    – dturvene
    Dec 23, 2013 at 20:10
  • 1
    This is the answer if you're using Eclipse. Mar 4, 2014 at 20:11
  • its same procedure in android studio but only difference is that you can not select theme May 30, 2014 at 2:11
24

In Android studio:

Right click the Drawable folder > Select New > Image Asset

(Alternatively, click shift, shift and type 'Image Asset")

In the dialog which popped up click 'OK'

From the wizard you can customise the asset you require. Choose if it's a Notification icon, Action bar icon or launcher icon. You can select icons from clipart, create icon from text or import an image from disk. The wizard will take care of creating different resolutions for your new image.

Android Studio Asset Wizard

19

You need an image called ic_action_search.png or ic_action_search.gif, for example. Paste this image in every res/drawable folder.

1
  • 1
    In addition, GIF file format is discouraged in Android. (GIF Animation is supported only partially.) PNG and JPG are better for Android. PNG (preferred), JPG (acceptable) and GIF (discouraged). developer.android.com/guide/topics/graphics/2d-graphics.html Android can also read BMP and WEBP file according to version.
    – Naetmul
    Aug 7, 2013 at 4:02
7

As others have advised, you need to have (put) the icon graphic, "ic_action_search.png" in the correct folder, which is "/res/drawable" You may have to move a graphic file for one or more of the "drawable" subfolders of "/res"

You can find the icon graphics (and others that you will, presumably, need in the future) at, (http://developer.android.com/design/downloads/index.html#action-bar-icon-pack)

Please note: do not use the "Core Icons/unstyled," in the package. Use the "All_Icons/holo_light," (most likely) or "All_icons/holo_dark."

5

The error means that you don't have an image called ic_action_search in your drawable folder.

You can download an image for search from Android Asset Studio if you like.

2

I suppose you copy pasted some example code, as other answers says you missed putting drawable into your resources. For testing you might use default android search icon:

android:icon="@android:drawable/ic_menu_search"

I am not sure if it is safe to use it in release apps

1

First think English is no my main language, i apologize for my typing. Ok let's go to the problem. All this works for me, i hope it works for you. I was following the same instructions from developer.android.com as you, and it didn't say that you have to put it in both .xml, or i didn't see.

Because is too much stuff and is overwhelming, i know at first but i hope to get used to it soon. Under the folder: res/menu you have two.xml archive one for the main page and the other for the activity. What do you have to do is copy that you wrote to both .xml archive:

Display_message.xml

main.xml

remember the action bar goes along the way with the activity, as many as you have. That means if you add another activity, you have to paste this in the new one in order to void that error. By the way you already have the images int he their respective folder. if not, it going to pop the error again. I hope this was understandable and with sense.

0

An other way to add a new icon into your project in Android Studio (same as dturvene's in Eclipse)

1. Choose drawable directory; Alt + Insert or File/New...
 2. Choose "Image Asset"
 3. Select drawable\hdpi as your icon's destination
 4. An option window will pop up. Select directory to your icon saved in your PC.

I just found the way to do it so I hope it help.

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.