Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am following this Tutorial but I can't get a simple search button to show up on the options menu. Code:

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="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" /> </menu>

Added this to MainActivity.java

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);

    }

App loads OK but no icons show up, I have downloaded standard icons and placed them in each of the drawable folders. Any ideas why is it not working?

Not sure if related but the tutorial states

"The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory. For example, "@drawable/ic_action_search" refers to ic_action_search.png."

But I didn't have folder res/drawable/ just res/drawable-hdpi, res/drawable-ldpi ... Shold I create another folder res/drawable/ if so which icon should go in it (the official icon set I downloaded had all the res/drawable-hdpi ones but no res/drawable/)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
467 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just Replace your code with mine and it will work for you.

<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.actionbar.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:title="@string/action_settings"
              app:showAsAction="never"/>



    </menu>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...