0

I have added a share button on action item, I am trying to handle click event on the action button (button in action bar). I tried with the below code but I know I have to add clicklistener for this. Below is my activity code :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.d("", "Inside the menu selected");
    System.out.println("Inside the menu selected");
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_share:
            shareImage();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

This is code for my menu layout :

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

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_settings"/>
    <item android:id="@+id/action_share"
        android:icon="@drawable/share2"
        android:title="search"
        android:showAsAction="always"  />
</menu>

I also tried to refer : onOptionsItemSelected() not called when clicking on menu item which has an actionLayout set on it

But I am not getting what is itemchooser in the answer.

Please help me on this.

Thanks, Aman

Community
  • 1
  • 1
Amandeep Singh
  • 3,754
  • 8
  • 51
  • 72

3 Answers3

0

I think the problem is

default:
        return super.onOptionsItemSelected(item);

You need add "return super.onOptionsItemSelected(item);" line after switch block.

Try this and tell me what happend. Hope this help you :)

Biswajit
  • 1,829
  • 1
  • 18
  • 33
0

Since you are inflating your own option menu and not using your default menu you should return true; from onCreateOptionsMenu.

Pr38y
  • 1,565
  • 13
  • 21
0

The problem here was the activity where I was writing the code was one of the tab activity which was hosted under main activity. I wrote the same code under the tabhost activity and it worked.

Thanks for your answers though.

Amandeep Singh
  • 3,754
  • 8
  • 51
  • 72