1

Im having trouble with the menu items.

I have a search view in my menu, and I set OnMenuItemClickListener on the item in my onPrepareOptionsMenu.

enter image description here

and my menu xml: the showAsAction attribute is set to "always" enter image description here

however, if I click the search icon, nothing happen, the toast did not show up.

enter image description here

strange thing is if I set showAsAction="always|collapseActionView", if will work, but the search icon is gone and replaced by "search" text.

it works, toast is shown.

enter image description here

but the icon is gone

enter image description here

********************edit****************************************

enter image description here

Qing
  • 1,401
  • 2
  • 21
  • 41

1 Answers1

2

You are using the wrong syntax for your menu. Kindly replace you menu inflater/handler code with this.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.yourActivity_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.search) {
        //Handle the onClick with Toast over here.
        //IF you have multiple options then replace if with a switch() statement
        return true;
    }

    return super.onOptionsItemSelected(item);
}

As far as the icon is concerned, you have specify the icon attribute in the xml file by adding android:icon="@drawable/YOU_ICON_HERE"

EDIT: Have a look at this http://developer.android.com/training/search/setup.html You are supposed to keep android:showAsAction="collapseActionView|ifRoom" for this to work. The only change you now have to make is add an android:icon attribute to the xml

<item android:id="@+id/search"
      android:title="@string/search_title"
      android:icon="@drawable/ic_search"
      android:showAsAction="collapseActionView|ifRoom"
      android:actionViewClass="android.widget.SearchView" />

or android.widget.v7 support depending on your android version you wish to support

Varun Agarwal
  • 1,587
  • 14
  • 29
  • Hi Varun, I used toolbar to replace the default actionbar. I tried your method, but its not working either... the onOptionsItemSelected() method is not called at all. Is it because I used toolbar? – Qing Sep 30 '15 at 07:02
  • Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setHomeUpEnabled(); Add these lines and show me your initial lines of onCreate() – Varun Agarwal Sep 30 '15 at 07:05
  • hi Varun, Ive updated my post for those codes. thank you – Qing Sep 30 '15 at 07:10
  • add if(getSupportActionBar!=null){ setHomeButtonEnabled(true) } to get rid of the null pointer error. Is it working now? Also you can change the initial icon, home button icon, Title and add a subtitle programmatically. – Varun Agarwal Sep 30 '15 at 07:16
  • hi ive added getSupportActionBar().setHomeButtonEnabled(true); and its not working still.... – Qing Sep 30 '15 at 07:20
  • Firstly, is the text "search" showing properly? Have you added the code for the icon? And when you click on it, is a ripple effect being observed? – Varun Agarwal Sep 30 '15 at 07:22
  • if I set the showAsAction to always, the search title is not showing but the defualt icon, but if i set to always|collapseActionView, it will show the title instead of the icon – Qing Sep 30 '15 at 07:29
  • Then just remove the Title attribute and see how it works. Google recommends to keep it as always|collapseActionView – Varun Agarwal Sep 30 '15 at 07:30
  • i cant remove the title attribute, if i remove it says "menu item should have a title attr".... the point is the showAsAction attribute, if always|collapseActionView it works but the icon is gone , title take place , if always only, the listener does not work – Qing Sep 30 '15 at 07:35
  • http://stackoverflow.com/questions/15422505/android-actionbar-collapsible-searchview-with-action-button Long solution but this OP has been able to achieve this. If it works do accept my answer :) – Varun Agarwal Sep 30 '15 at 07:40
  • thats not what im concerned, im wondering why the showAsAction will resutl in the disability of the onClicked method....anyway...i upvote your answer, – Qing Sep 30 '15 at 07:43