Android menu usage

Source: Internet
Author: User

1. Normal menu

Let's take a look at how to implement the simplest menu.

Override the oncreateoptionsmenu (menu) method in the main activity.

Code

 @ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Menu. Add (0, 1, 1, "apple ");
Menu. Add (0, 2, 2, "banana ");
Return super. oncreateoptionsmenu (menu );
}

In this way, two menu options are available. To add a click event, override the onoptionsitemselected (menuitem item) method.

Code

 @ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
If (item. getitemid () = 1 ){
Toast T = toast. maketext (this, "you selected Apple", Toast. length_short );
T. Show ();
}
Else if (item. getitemid () = 2 ){
Toast T = toast. maketext (this, "you selected bananas", Toast. length_short );
T. Show ();
}
Return true;
}

 

2. Submenu

Submenu creation is also simple. Add a few sentences to the oncreateoptionsmenu (menu) method of the first code, as follows:

Code

 @ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Menu. Add (0, 1, 1, "apple ");
Menu. Add (0, 2, 2, "banana ");
Submenu = menu. addsubmenu (1,100,100, "Peach ");
Submenu. Add (2,101,101, "Peach ");
Submenu. Add (2,102,102, "Peach ");
Return true;
}

After you click "Peach", the sub menu will appear. There are two sub options: "big peach" and "little peach ".

3. Context Menu

It is similar to the shortcut menu on the computer that pops up after a view is long pressed.

First, define several buttons in Main. xml. Then override the oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo) method. The Code is as follows:

First, you must register the following code in the oncreate method:

Code

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
registerForContextMenu(b1);
registerForContextMenu(b2);
}

 

Code

 @ Override
Public void oncreatecontextmenu (contextmenu menu, view V,
Contextmenuinfo menuinfo ){
// Todo auto-generated method stub
If (V = b1 ){
Menu. setheadertitle ("this is 1 ");
Menu. Add (200,200,200, "context menu 1 ");
Menu. Add (200,201,201, "context menu 2 ");
}
Else if (V = b2 ){
Menu. setheadertitle ("this is 2 ");
Menu. Add (300,300,300, "C 1 ");
Menu. Add (300,301,301, "C 2 ");
}
Super. oncreatecontextmenu (menu, V, menuinfo );
}

4. Dynamic menu

Dynamic menus are different based on different interfaces. The following code implements this function: when the value of a textview on the main interface is "M" and "N", different menus are displayed.

Code

 @ Override
Public Boolean onprepareoptionsmenu (menu ){
// Todo auto-generated method stub
String currenttext = tv1.gettext (). tostring ();
If ("M". Equals (currenttext )){
Menu. Clear (); // clear the menu first
Menuitem item = menu. Add (0,400,401, "to N"); // you can click this menu item to change the value of TV1 so that it can be tested.
Item. seticon (Android. R. drawable. alert_dark_frame); // the built-in Android icon
}
If ("N". Equals (currenttext )){
Menu. Clear (); // clear the menu first
Menuitem item = menu. Add (0,401,402, "to M"); // you can click this menu item to change the value of TV1 so that it can be tested.
Item. seticon (Android. R. drawable. alert_light_frame );
}
Menu. Add (0,402,403, "now is" + currenttext); // There are currently two menu items
Return super. onprepareoptionsmenu (menu );
}

  

5. Create a menu using an XML file

Previously, menus were created using code, and menus can be easily created using xml configuration files.

To create a folder named menu under the Res/directory, create an XML file named menu_xml_file.xml. The Code is as follows:

Code

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/grout_main">
<item android:id="@+id/menu_1"
android:title="This 1"/>
<item android:id="@+id/menu_2"
android:title="This 2" />
</group>
</menu>

Override the oncreateoptionsmenu (menu) method in the activity. The Code is as follows:

Code

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_xml_file, menu);
return true;
}

Others are the same as creating menus in the activity.

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.