Showing posts with label Menu. Show all posts
Showing posts with label Menu. Show all posts

onContextItemSelected - Perform Action on Context Menu Selection

public boolean onContextItemSelected (MenuItem item) was added in API level 1
This hook is called whenever an item in a context 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. Use getMenuInfo() to get extra information set by the View that added this menu item.
Derived classes should call through to the base class for it to perform the default menu handling.
Parameter: item - The context menu item that was selected.
Returns: boolean - Return false to allow normal context menu processing to proceed, true to consume it here.
This is the method that is called when a context menu item is selected.

1. Make a Context Menu for a ListView Item

2. In the MainActivity.java file, add the onContextItemSelected method to the class.

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.myitem:
            // do something. To test, Add a Toast
            return true;
    }

    return super.onContextItemSelected(item);
}

3. Compile and run!

Resources:
http://developer.android.com/reference/android/app/Activity.html#onContextItemSelected(android.view.MenuItem)

How to Make a Context Menu for a ListView Item


1. In the MainActivity.java file, add an Action on Long Press on ListView ListItem

2. Inflate a Menu in the onCreateContextMenu method. Do not include the icon line in the XML file. A context menu does not support icons because they are simply a list of menu options that floats above the current activity.

3. Perform Action on Context Menu Selection

4. Compile and run!

Resources:

MenuInflater - How to Inflate a Menu

MenuInflator was added in API Level 1
This class is used to instantiate menu XML files into Menu objects.

1 Create a Menu XML file

2. In the MainActivity.java file, add the below line to the imports section.

import android.view.MenuInflater;
import android.view.InflateException;


3. Add the below lines to your desired method. getMenuInflater() returns a MenuInflater with this context.

MenuInflater myMenuInflater = getMenuInflater();
try {
    myMenuInflater.inflate(R.menu.mymenu, menu);
} catch ( InflateException e) {
    e.printStackTrace();
}

4. Compile and run!

Resources:
http://developer.android.com/reference/android/view/MenuInflater.html#inflate(int, android.view.Menu)
http://developer.android.com/reference/android/app/Activity.html#getMenuInflater()

onMenuItemSelected - Create Action when Menu Item is Pressed

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)


onCreateOptionsMenu - Create an Options Menu

public boolean onCreateOptionsMenu (Menu menu) was added in API level 1
Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu.
This is only called once, the first time the options menu is displayed. To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu).
The default implementation populates the menu with standard system menu items. These are placed in theCATEGORY_SYSTEM group so that they will be correctly ordered with application-defined menu items. Deriving classes should always call through to the base implementation.
You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.
When you add items to the menu, you can implement the Activity's onOptionsItemSelected(MenuItem) method to handle them there.
Parameters: menu - The options menu in which you place your items.
Returns: You must return true for the menu to be displayed; if you return false it will not be shown.

1. In the MainActivity.java file, add the below line to the imports section.

import android.view.Menu;

2. Add the below method to your class, after the other methods.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  return true;
}

3. Inflate a Menu named mymenu in the onCreateOptionsMenu method before the return true line of code. This will inflate the file in res/menu named mymenu.

4. Compile and run!
Next Recommended Article: onMenuItemSelected - Create Action when Menu Item is Pressed Resources: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)


Create a Menu XML file

1. Create a menu directory in the res directory.

2. Add a file named of mymenu.xml to the menu directory. An item named myItem is created. An icon named ic_menu_add is used, from the default Android platform icons, with a visible text of "My Item"

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/myItem"
        android:icon="@android:drawable/ic_menu_add"
        android:title="My Item" />
</menu>

3. Compile and run!

4. For more items, you can add more items under the first item, befor the </menu> tag, to the above code. Such as,

    <item android:id="@+id/myItem2"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="My Item 2" />

Next Recommended Article: onCreateOptionsMenu - Create an Options Menu

Resources: