Context menu of ContexMenu for Android from scratch

Source: Internet
Author: User

What is context menu:
Sometimes we can also call it a shortcut menu. For example, the shortcut menu that we right-click on the desktop is a shortcut menu, also called Context menu. It is called Context menu because Context translates into Context
To create a context ContextMenu:
1. overwrite the onCreateContextMenu () method of the Activity and call the add method of Menu to add a Menu item.
2. overwrite the onContexItemSelected () method, and click the event in the response menu.
3. Call registerForContexMenu () to register the context menu for Vision
Public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu. ContextMenuInfo menuInfo)
Parameter description:
Menu: shortcut menu to be displayed
V: V is the user-selected interface element.
MenuInfo: menuInfo is the additional information of the selected interface element.
Note: The onCreateContextMenu function is different from the onCreateOptionsMenu function. The onCreateOptionsMenu function is called only once when the option menu is started for the first time, and the onCreateContextMenu function is called once at each start.
 
Public boolean onContextItemSelected (MenuItem item)
This method is similar to the previous onMenuItemSelected method.
 
Instance 1:

Package com. jiahui. activity;
 
Import android. app. Activity;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. view. ContextMenu;
Import android. view. Menu;
Import android. view. MenuInflater;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. widget. TextView;
 
Public class ContextMenuDemoActivity extends Activity {
 
Private static final int ITEM1 = Menu. FIRST;
 
Private static final int ITEM2 = Menu. FIRST + 1;
 
Private static final int ITEM3 = Menu. FIRST + 2;
 
Private TextView myTxt;
 
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
MyTxt = (TextView) findViewById (R. id. mytxt );
 
// Register the context menu with the TextView Control
RegisterForContextMenu (myTxt );
}
 
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
 
// Add a menu item
Menu. add (0, ITEM1, 0, "red background ");
Menu. add (0, ITEM2, 0, "green background ");
Menu. add (0, ITEM3, 0, "white background ");

}
 
// You can select different buttons to change the background color of TextView.
@ Override
Public boolean onContextItemSelected (MenuItem item ){
 
Switch (item. getItemId ()){
Case ITEM1:
MyTxt. setBackgroundColor (Color. RED );
Break;
Case ITEM2:
MyTxt. setBackgroundColor (Color. GREEN );
Break;
Case ITEM3:
MyTxt. setBackgroundColor (Color. WHITE );
Break;
 
}
Return true;
}
 
}

 

<? 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: id = "@ + id/mytxt" android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
 
</LinearLayout>

 
Effect:

 

Example 2: use XML to create a context menu
You need to return a MenuInflater object through getMenuInflater () of the Activity, and then specify the reference of the XML file through the inflater () method of the MenuInflater object
Public void inflate (int menuRes, Menu menu)
Parameter description:
MenuRes: reference location of an XML file
Menu: the menu to display
 

Package com. jiahui. activity;
 
Import android. app. Activity;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. view. ContextMenu;
Import android. view. Menu;
Import android. view. MenuInflater;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ContextMenu. ContextMenuInfo;
Import android. widget. TextView;
 
Public class ContextMenuDemoActivity extends Activity {
 
Private static final int ITEM1 = Menu. FIRST;
 
Private static final int ITEM2 = Menu. FIRST + 1;
 
Private static final int ITEM3 = Menu. FIRST + 2;
 
Private TextView myTxt;
 
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
MyTxt = (TextView) findViewById (R. id. mytxt );
 
// Register the context menu with the TextView Control
RegisterForContextMenu (myTxt );
}
 
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
 

// Menu items created in XML format
MenuInflater inflater = getMenuInflater ();

Inflater. inflate (R. menu. context_menu, menu );
 
}
 
// You can select different buttons to change the background color of TextView.
@ Override
Public boolean onContextItemSelected (MenuItem item ){
 
Switch (item. getItemId ()){
Case ITEM1:
MyTxt. setBackgroundColor (Color. RED );
Break;
Case ITEM2:
MyTxt. setBackgroundColor (Color. GREEN );
Break;
Case ITEM3:
MyTxt. setBackgroundColor (Color. WHITE );
Break;
 
}
Return true;
}
 
}
 
Context_menu.xml:

<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/contextMenu1" android: title = "menu item 1 created in XML"> </item>
<Item android: id = "@ + id/contextMenu2" android: title = "menu item 2 created in XML"> </item>
<Item android: id = "@ + id/contextMenu2" android: title = "menu item 3 created in XML"> </item>
</Menu>
 
Effect:

From: jiahui524 Column
 

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.