Android Development Series (28): Create an option menu with submenu

Source: Internet
Author: User

Most phones have a "menu" key on the top, and after an app is installed on your phone, you can display the menu that the app is associated with by using "menu".

However, starting with Android 3.0, Android no longer requires a menu case on mobile devices, although many phones now offer menu keys, but some are no longer available. In this case, Android recommends using Actionbar instead of the menu. We'll cover Android's support for Actionbar in a future blog post.

The menu interface is one of the keys that implements this function, where submenu inherits from the menu parent interface

Next we use a specific application to illustrate, most of the methods will be used:

Let's start by creating a Main.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><edittext android:id= "@+id/txt" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Content for testing" android:editable= "false"/></linearlayout>
In this file, we define a line of text, and then we will change the font and change the font color through menus and submenus.

Next, let's look at

Import;import;import Android.os.bundle;import Android.view.Menu;import Android.view.menuitem;import Android.view.submenu;import Android.widget.edittext;import android.widget.Toast; public class Menutest extends activity{//defines the font Size menu item's identity final int font_10 = 0x111;final int Font_12 = 0x112;final int font_1 4 = 0x113;final int font_16 = 0x114;final int font_18 = 0x115;//defines the identity of the generic menu item final int plain_item = 0x11b;//Define font Color The identity of the menu item fin Al int font_red = 0x116;final int font_blue = 0x117;final int font_green = 0x118;private EditText edit; @Overridepublic voi D onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); edit = ( EditText) Findviewbyid (;} This method is triggered when the user clicks the menu key @overridepublic boolean oncreateoptionsmenu (Menu menu) {//------------- Add a font-size submenu to the menu-------------submenu Fontmenu = Menu.addsubmenu ("Font size");//The icon for the Settings menu Fontmenu.seticon (r.drawable.font )///Set Menu header icon Fontmenu.setheadericon (r.drawable.font);//Set Menu headerTitle Fontmenu.setheadertitle ("Select font Size"), Fontmenu.add (0, font_10, 0, "10th"), Fontmenu.add (0, Font_12, 0, "12th font"); Fontmenu.add (0, font_14, 0, "14th font"), Fontmenu.add (0, font_16, 0, "16th font"), Fontmenu.add (0, font_18, 0, "18th");//------ -------Add a normal menu item to the menu-------------menu.add (0, Plain_item, 0, "General menu item");//------------- Add a submenu of text color to the menu-------------submenu Colormenu = menu.addsubmenu ("Font Color"); Colormenu.seticon (r.drawable.color);// Set the icon for the menu header Colormenu.setheadericon (r.drawable.color);//Set the header of the menu header Colormenu.setheadertitle ("Select text color"); Colormenu.add (0 , font_red, 0, "Red"), Colormenu.add (0, Font_green, 0, "green"), Colormenu.add (0, Font_blue, 0, "Blue"); return Super.oncreateoptionsmenu (menu);} @Override//Options Menu menu item The callback method that is clicked after the public boolean onoptionsitemselected (MenuItem mi) {//determines which menu item is clicked and responds accordingly. Switch (Mi.getitemid ()) {case FONT_10:edit.setTextSize (2), Break;case FONT_12:edit.setTextSize (2); break;case FONT_14:edit.setTextSize (2); Break;case FONT_16:edit.setTextSize (2); Break;case FONT_18:edit.setTExtsize (2); Break;case FONT_RED:edit.setTextColor (; Break;case FONT_GREEN:edit.setTextColor (; break;case FONT_BLUE:edit.setTextColor (Color.Blue); break;case plain_item:toast Toast = Toast.maketext ( Menutest.this, "You clicked on the ordinary menu item", toast.length_short); (); break;} return true;}}
First, we rewrote the Oncreateoptionsmenu (Menu menu) method, which triggers this method when the user taps the menu key:

The menu here is the one that corresponds to when you press menu, we have added two menu "Font size", "Font Color" through Menu.addsubmenu (charsequence title), and through Add (int titleres) Added a regular menu item

We then add options for font size, font color, respectively.

Let's see:

We first click on the "menu" key to get the bottom:

Then, when we click on "Font Size", it will appear below:

By clicking on one of these options, you can change the font of the text.

Android Development Series (28): Create an option menu with submenu

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: 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.