0

This is my first time making an Android Actionbar. I just want to add a search image in the bar. But for some reason the image button is not appearing. Here is the Actionbar XML:

<?xml version="1.0" encoding="utf-8"?>
<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, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />

</menu>

I put showAsAction as true, so the image should appear.

MainActivtiy.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);
    }

Here is the screen:

enter image description here

Clearly the screen large enough.

enter image description here

and I have the images in the folders. I'm not sure why its not appearing?

user481610
  • 3,230
  • 4
  • 54
  • 101
  • are you using appcompat? – Raghunandan Jun 28 '14 at 15:39
  • I'm not to sure what that means (I just started Android today) but yes when I made the project appcompat_v7 appeared – user481610 Jun 28 '14 at 15:40
  • does your activity extend ActionBarActivity. Are you using support library? If so the first part of the link should answer. http://stackoverflow.com/questions/24404286/not-able-to-get-the-overflow-feature-for-actionbar-in-android/24404333#24404333 – Raghunandan Jun 28 '14 at 15:41

1 Answers1

1

the following code worked for me this was my menu file res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" > <!-- this line matters -->

  <item
     android:id="@+id/searchItemMenu"
     android:icon="@drawable/action_search"
     yourapp:showAsAction="ifRoom|withText"  <!-- use it here -->
     android:title="@string/new_search"/>

</menu>

and in java file

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

Hope this works for you.

Kaustubh
  • 653
  • 6
  • 21