3

Dear can we use onclick with menu item in xml. like below .i tried it but its not working.

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

<item
    android:id="@+id/logout"
    android:onClick="onLogOut"
    android:title="@string/logout">
</item>

Java Code :

public void onLogOut(MenuItem v) {
    Utility.LogError(TAG, "onLogOut Clicked");
    Toast.makeText(this, "onLogOut", Toast.LENGTH_SHORT).show();
}

}

onLogOut is not get called....

DroidEngineer
  • 467
  • 2
  • 9
  • 20
  • Where is your `onLogOut()` function located? Because they need to be in the activity. Unfortunately you can't have them in Fragments – Cassie Dec 05 '12 at 03:41
  • Yup i put it in activity and and change its signature too but its still not get called . new signature is given below public boolean onLogOut(MenuItem v) {} now it return boolean. Still not get called.:) – DroidEngineer Dec 05 '12 at 03:48
  • i Just Noticed OS is also throwing some exceptional information like 12-05 08:50:00.509: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45086d80 http://stackoverflow.com/questions/3140524/geting-window-already-focused-ignoring-focus-gain-of-com-android-internal-vie – DroidEngineer Dec 05 '12 at 03:51
  • 1
    This feature wasn't introduced for `MenuItem` until API Level 11 - any chance you're running this on an older device? – MH. Dec 05 '12 at 06:00

3 Answers3

7

use

android:onClick="onLogOutClick"

instead of

android:onClick="@string/onLogOutClick"

for adding onclick with menu item

and And in Java we should write the method for this onlick

public void onLogOutClick(MenuItem item) {
     Log.d("MenuItem", "onLogOutClick :: "+item.getItemId());  
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

Have you tried it already to make it work with the onOptionItemSelected() function?

onOptionsItemSelected

Added in API level 1

boolean onOptionsItemSelected (MenuItem item)

This hook is called whenever an item in your options menu is selected. The default implementation simply returns false to have the normal processing happen (calling the item's Runnable or sending a message to its Handler as appropriate). You can use this method for any items for which you would like to do processing without those other facilities.

Edit:

I also found this for Handling click events

Handling click events

To perform an action when the user selects a menu item, you must implement the PopupMenu.OnMenuItemClickListener interface and register it with your PopupMenu by calling setOnMenuItemclickListener(). When the user selects an item, the system calls the onMenuItemClick() callback in your interface.

For example (example is from the website):

 public void showMenu(View v) {
    PopupMenu popup = new PopupMenu(this, v);

    // This activity implements OnMenuItemClickListener
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.actions);
    popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.archive:
            archive(item);
            return true;
        case R.id.delete:
            delete(item);
            return true;
        default:
            return false;
    }
}

So probably if you inset the onOptionsItemSelected or the onMenuItemClick you should be able to receive a call from the onLogOut.

Crazmiss
  • 66
  • 5
0

That's because that only works for XML layouts, that there is a menu. For menus you should override the onItemMenuSelected() method. Read the documentation.

Edit:

Whoops! Apparently it is possible but only with Honeycomb and later.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Yup you might be right but [link](http://developer.android.com/guide/topics/resources/menu-resource.html) Documentation here its mentioned that you can also provide method in XML. – DroidEngineer Dec 05 '12 at 03:56
  • Ah, huh, this is new to me, thanks! Apparently this is real but was "Introduced in API Level 11." – dmon Dec 05 '12 at 14:59