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
- Perform an Action after Enter keypress on EditText
- Intent setType(String type) - How to Set an Explicit MIME data type
- android:ellipsize - Add an ellipsis(...) in a TextView
- Create a Drawable Object Variable
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- Solution: Running Android Lint has encountered a problem Failed
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText
- MenuInflater - How to Inflate a Menu
- How to Declare a new HttpURLConnection
- android:scrollbarStyle - Change the Scrollbar Location/Style in a TextView