public boolean onMenuItemSelected (int featureId, MenuItem item) was added in API level 1
Default implementation of onMenuItemSelected(int, MenuItem) for activities. This calls through to the newonOptionsItemSelected(MenuItem) method for the FEATURE_OPTIONS_PANEL panel, so that subclasses of Activity don't need to deal with feature codes.
Parameters:
featureId - The panel that the menu is in.
item - The menu item that was selected.
Returns: boolean - Return true to finish processing of selection, or false to perform the normal menu handling (calling its Runnable or sending a Message to its target Handler).
1. Create an Options Menu
2. In the MainActivity.java file, add the below line to the import section.
import android.view.MenuItem;
3. Add the method below to the MainActivity class. This creates an action for the menu item named myItem, which must be same as the item named in the menu.xml file, named android:id="@+id/myItem".
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.myItem:
//Do something here. To test it you could Add a Toast, or Open a New Activity/Screen
return true;
}
return super.onMenuItemSelected(featureId, item);
}
4. Compile and run!
5. To add more actions to the items, just add more case statements after the case statement above.
Resources:
http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected(int, android.view.MenuItem)
Popular Posts
- android:ellipsize - Add an ellipsis(...) in a TextView
- android:textColorHighlight - Set the Color of Highlighted Text in an EditText
- Perform an Action after Enter keypress on EditText
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- How to Import a Library
- Save LogCat to a Text File
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText
- android:drawablePadding - Add Padding to a Drawable in a TextView, in XML
- How to Set Permission for I/O Operations over NFC
- How to Install an SDK package