Context menu : Menu « UI « Android






Context menu

  

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Test extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Register a button for context events
        Button button = new Button(this);
        button.setText("Click for Options");
        button.setOnClickListener(listener);
        registerForContextMenu(button);

        setContentView(button);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {
            openContextMenu(v);
        }
    };

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.contextmenu, menu);
        menu.setHeaderTitle("Choose an Option");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //Switch on the item? ID to find the action the user selected
        switch(item.getItemId()) {
        case R.id.menu_delete:
            //Perform delete actions
            return true;
        case R.id.menu_copy:
            //Perform copy actions
            return true;
        case R.id.menu_edit:
            //Perform edit actions
            return true;
        }
        return super.onContextItemSelected(item);
    }

}

//contextmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/menu_delete"
    android:title="Delete Item"
  />
  <item
    android:id="@+id/menu_copy"
    android:title="Copy Item"
  />
  <item
    android:id="@+id/menu_edit"
    android:title="Edit Item"
  />
</menu>

   
    
  








Related examples in the same category

1.Create Option menu
2.Menu and messages
3.Custom menu
4.Using Icon in Menu
5.Option Menu selection event
6.Menu Inflation
7.Context Menu event
8.Create Menu within your code
9.Activity Menu
10.Define menu in xml file
11.Adding submenu
12.Using Icon in menu and submenu
13.Get Menu title
14.onOptionsItemSelected/onPrepareOptionsMenu/onCreateOptionsMenu Event
15.Basics of the Action Bar and how it interoperates with the standard options menu.
16.Demonstration of displaying a context menu from a fragment.
17.Demonstrates how fragments can participate in the options menu.
18.Demonstrates inflating menus from XML.
19.Usage of SearchView in an ActionBar as a menu item.
20.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL