Context menu option in Android -- ContextMenu

Source: Internet
Author: User

In the Android system, the ContextMenu (context menu) is similar to the right-click pop-up menu in the PC. When a view is registered to a context menu, a "Long press" action on the object is executed, A floating menu that provides relevant functions will appear. Context menu can be registered to any view object. However, the most common option is the list view ListView item, the background color is converted and the context menu is displayed.

Note: context menus do not support icons and shortcut keys.

To create a context menu, you must override the context menu callback functions of the activity: onCreateContextMenu () and onContextItemSelected (). In the onCreateContextMenu () callback function, you can add menu items by using an add () method, or expand a menu resource defined in XML. Then, register a context menu ContextMenu for this view through registerForContextMenu.

Next, I will use an instance Demo to demonstrate the basic usage of ContextMenu. You can configure Menu options by manually adding Menu and XML files.

[1] The project structure directory of the Demo is as follows:

 

[2] the source code of the main. xml layout file under the res/layout directory is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Hello, This is Andy's Blog! "/>
<ListView
Android: id = "@ + id/lv"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>

[3] The source code of the cmenu. xml menu option file under the res/menu directory is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu
Xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/add" android: title = "add"/>
<Item android: id = "@ + id/update" android: title = "update"/>
<Item android: id = "@ + id/delete" android: title = "delete"/>
</Menu>

[4] MenuActivity. java source code under the com. andyidea. menudemo package is as follows:

Package com. andyidea. menudemo;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. ContextMenu;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. view. Menu;
Import android. view. MenuInflater;
Import android. view. MenuItem;
Import android. view. View;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
Import android. widget. Toast;
 
Public class MenuActivity extends Activity {

ListView lv;
Private ArrayAdapter <String> adapter;
Private List <String> alist = new ArrayList <String> ();

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Lv = (ListView) findViewById (R. id. lv );
Alist. add ("first ");
Alist. add ("second ");
Alist. add ("third ");
Adapter = new ArrayAdapter <String> (this, android. R. layout. simple_expandable_list_item_1, alist );
Lv. setAdapter (adapter );

// Register the view object, that is, registering the context menu for the ListView Control
RegisterForContextMenu (lv );
}
 
/**
* Create context menu options
*/
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){

// 1. manually add context menu options
// Menu. add (0, 1, 0, "modify ");
// Menu. add (0, 2, 0, "delete ");

// 2. Configure context menu options through the xml file
MenuInflater mInflater = getMenuInflater ();
MInflater. inflate (R. menu. cmenu, menu );

Super. onCreateContextMenu (menu, v, menuInfo );
}
 
/**
* This method is called when an option in the menu is clicked.
*/
@ Override
Public boolean onContextItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case 1:
Toast. makeText (this, "you have selected manual modification", Toast. LENGTH_SHORT). show ();
Break;
Case 2:
Toast. makeText (this, "you have selected manual deletion", Toast. LENGTH_SHORT). show ();
Break;
Case R. id. add:
Toast. makeText (this, "you have selected XML to add", Toast. LENGTH_SHORT). show ();
Break;
Case R. id. update:
Toast. makeText (this, "you have selected XML Update", Toast. LENGTH_SHORT). show ();
Break;
Case R. id. delete:
Toast. makeText (this, "you have selected XML to delete", Toast. LENGTH_SHORT). show ();
Break;
}
Return super. onContextItemSelected (item );
}
 
/**
* Method called when the context menu is disabled
*/
@ Override
Public void onContextMenuClosed (Menu menu ){
// TODO Auto-generated method stub
Super. onContextMenuClosed (menu );
}

}

[5] The Demo runs as follows:

 

From: Android-Idea

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.