Android uses menu in activity

Source: Internet
Author: User

Mobile screens are very limited, so make the most of your screen space. Android offers a menu that allows the menu to be fully displayed, and does not occupy any screen space.

So how do you create a menu?

1. In the Res directory, create a new menu folder, under the folder to build a file called Main.

2. Add the following code to the Main.xml:

1 <2     xmlns:android= "Http://schemas.android.com/apk/res/android" >3     <item4         android:id= "@+id/add_item"5         android:title= "Add"/>6      <item7         android:id= "@+id/remove_item"8         android:title= "Remove"/>9 </menu>
View Code

There are two menu items created here, where the <item> tag is used to create a specific menu item that specifies a unique identifier for the menu item by ID, and the title assigns a unique name to the menu item.

3. Override the Oncreateoptionsmenu () method in the activity where the menu item is located (override method shortcut Ctrl+o)

1  Public Boolean Oncreateoptionsmenu (Menu menu) {2     getmenuinflater (). Inflate (R.menu.main,menu); 3      return true ;                 4 }
View Code

The Getmenuinflater () method can be used to get the Menuinflater object of the activity, and then call its inflate () method to set menu in menu XML as the activity's menus. You can create a menu for the current activity.

The inflate () method accepts two parameters,

The first parameter is used to specify which resource file to create the menu from;

The second parameter specifies the menu object to which it will be added, where you can directly use the menu parameter passed in the method.

The method then returns True, which indicates that the menu created is allowed to be displayed, and if False is returned, the created menu cannot be displayed.

4. The menu definition is not for the sake of viewing, we can define the response event for the menu.

Override the Onoptionsitemselected () method:

1  Public Booleanonoptionsitemselected (MenuItem item) {2     Switch(item.getitemid) {3           CaseR.id.add_item:4Toast.maketext ( This, "You clicked Add", Toast.length_short). Show ();5                 Break;6           CaseR.id.remove_item:7Toast.maketext ( This, "You clicked Remove", Toast.length_short). Show ();8                 Break;9           default:Ten     }       One      return true; A}
View Code

Determine which menu item is clicked by calling Item.getitemid ().

After adding the menu and running the program again, you will find a three-point symbol on the right side of the title bar, which is the menu button.

Android uses menu in activity

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.