12

I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.

I inflate the action bar menu with this on each activity I use it:

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

        return true;
    }

And my xml for main2.xml is:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_searchHome"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Seach"/>



</menu>

My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?

Mike
  • 6,751
  • 23
  • 75
  • 132
  • 4
    I think you might be looking for [onOptionsItemSelected](https://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected(android.view.MenuItem)) – adneal Jul 01 '13 at 02:58
  • Check my comment under Eghdk reply, http://stackoverflow.com/questions/17396870/actionbar-menu-item-onclick#comment-25260264 – LuckyMe Jul 01 '13 at 05:01

2 Answers2

32

If you add an onClick attribute on your menu item like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_searchHome"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:onClick="doThis"
        android:title="Seach"/>



</menu>

Then in your activity:

public void doThis(MenuItem item){
    Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}

Note:

ActionBarSherlock is deprecated. Unless you are developing an app for Android 4.0 or older, please don't use it. But if you are using the library, you will have to import

import com.actionbarsherlock.view.MenuItem;

and not

import com.android.view.MenuItem;

In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick

which @adneal mentions.

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • 3
    No this is not the way to do this, `AcitonBars` have their own method to deal with clicks. Look here: http://developer.android.com/guide/topics/ui/menus.html#options-menu – LuckyMe Jul 01 '13 at 04:59
  • Both ways work. Also, I mentioned the method you just listed at the end of my answer. – EGHDK Jul 01 '13 at 23:02
  • 5
    Yes, technically, both can work, but one way is the the right way and the other is a terrible terrible way. it creates chaos and incoherence in the code. Please for the love of God, never use this method for Menu items. – LuckyMe Jul 01 '13 at 23:32
  • @LuckyMe why? this is typical command binding. .NET developers do it all the time :) – deviant Jan 17 '17 at 11:36
0

In my opinion

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onCreateDialog(getTaskId());
        }
    });
}


<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=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/add_text_id" android:title="Add"
    android:icon="@drawable/ic_add_btn"
    android:orderInCategory="100" app:showAsAction="ifRoom" />

josliber
  • 43,891
  • 12
  • 98
  • 133