I am working through the Android tutorial and cannot get my search icon to appear in my action bar.

I'm not sure what I am missing, but am new so I don't know where to look.

This is in my main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  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="always" />

    <!--  Settings, hsould awlays be in teh overflow -->
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="never" />
</menu>

This is in my MainActivity.java file:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

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

I have downloaded the ic_action_search.png files and placed them into the following respective directories:

/home/user/workspace/MyFirstApp/res/drawable-hdpi
/home/user/workspace/MyFirstApp/res/drawable-mdpi
/home/user/workspace/MyFirstApp/res/drawable-xhdpi
/home/user/workspace/MyFirstApp/res/drawable-xxhdpi

When I run the application via Eclipse, I do see the following error in the logcat console, but I'm not sure if they are referring to the png files or not?

<snip>
05-11 21:51:17.823: I/Adreno200-EGLSUB(32725): <ConfigWindowMatch:2087>: Format RGBA_8888.
05-11 21:51:17.833: D/memalloc(32725): ion: Mapped buffer base:0x506af000 size:1949696 offset:0 fd:59
05-11 21:51:17.833: E/(32725): Can't open file for reading
05-11 21:51:17.833: E/(32725): Can't open file for reading
05-11 21:51:17.843: D/OpenGLRenderer(32725): Enabling debug mode 0
<snip>

Where else can I check to determine why the search icon will not appear in the Action Bar?

** EDIT **

Following jay's recommendation below I've changed the main_activity_actions.xml to the following. This puts the string value into the Action bar, but not the icon. Does android know to look in the image folders automatically?

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:yourapp="http://schemas.android.com/apk/res-auto">
    <!--  Search, should appear as action button -->
    <item android:id="@+id/action_search"
        yourapp:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        yourapp:showAsAction="always" />

    <!--  Settings, hsould awlays be in teh overflow -->
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="never" />
</menu>
有帮助吗?

解决方案

if you miss xmlns:yourapp="http://schemas.android.com/apk/res-auto" this line to your menu then image cant get check it.

其他提示

I also had this issue, while trying to follow a tutorial. I tried all the solutions shown, including adding

xmlns:yourapp="http://schemas.android.com/apk/res-auto"

Nothing worked. My icon would still not display in the ActionBar What I finally noticed was that the tutorial had 3 versions of the styles.xml file, whereas my project only had a single styles.xml.

The additional styles were this: (styles.xml) in the res\values-v11 folder:

<resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 11 theme customizations can go here. -->
</style>

and this, in the res\values-v14 folder:

<resources>

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

when I dropped these two files in my res folder, it worked.

The best thing to do would be to download the search icon or for that reason, any icon from google developer site itself and modify accordingly.

https://www.google.com/design/icons/#ic_search

save the icon image in the res/drawable folder and refresh Android Studio.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top