{"id":11422,"date":"2014-07-29T11:00:08","date_gmt":"2014-07-29T08:00:08","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=11422"},"modified":"2019-04-24T16:05:32","modified_gmt":"2019-04-24T13:05:32","slug":"android-menu-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/","title":{"rendered":"Android Menu Example"},"content":{"rendered":"<p>Today we &#8216;re gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from other similars, is that we &#8216;re using the latest Android APIs, where Menus are manipulated differently.<\/p>\n<p>Having created an Android project, using the latest platform, gives us by default a menu layout, as we can see:<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<p><figure id=\"attachment_11423\" aria-describedby=\"caption-attachment-11423\" style=\"width: 306px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/default_menu_layout.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/default_menu_layout.png\" alt=\"Figure 1. Default menu layout of an Android application\" width=\"306\" height=\"182\" class=\"size-full wp-image-11423\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/default_menu_layout.png 306w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/default_menu_layout-300x178.png 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/a><figcaption id=\"caption-attachment-11423\" class=\"wp-caption-text\">Figure 1. Default menu layout of an Android application<\/figcaption><\/figure><\/p>\n<\/p>\n<h2>1. Introduction<\/h2>\n<p>For those who are more curious about what this contains, have a look at the <code>menu.xml<\/code>:<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>main.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml; highlight: [2, 10]\">&lt;menu xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    tools:context=\"com.javacodegeeks.android.menu.MainActivity\" &gt;\n\n    &lt;item\n        android:id=\"@+id\/action_settings\"\n        android:orderInCategory=\"100\"\n        android:title=\"@string\/action_settings\"\n        app:showAsAction=\"never\"\/&gt;\n\n&lt;\/menu&gt;\n<\/pre>\n<p>Let&#8217;s pretend that we don&#8217;t really know anything about <a href=\"http:\/\/developer.android.com\/guide\/topics\/ui\/menus.html\" target=\"_blank\" rel=\"noopener noreferrer\">menus<\/a>, so when running the app on our emulator, if we press the hardware &#8220;Menu&#8221; button, we should get something like this:<\/p>\n<p><figure id=\"attachment_11427\" aria-describedby=\"caption-attachment-11427\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/initial_menu.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/initial_menu.png\" alt=\"Figure 2. Initial state of an Android menu\" width=\"488\" height=\"470\" class=\"size-full wp-image-11427\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/initial_menu.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/initial_menu-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-11427\" class=\"wp-caption-text\">Figure 2. Initial state of an Android menu<\/figcaption><\/figure><\/p>\n<p>That is, a <code>menu<\/code> tag defines a menu, while an <code>item<\/code> tag defines different <code>menu<\/code> ite elements (i.e &#8220;Help&#8221;, &#8220;About&#8221;, etc.).<\/p>\n<p>Creating a custom <code>menu<\/code> in Android is very easy, so, don&#8217;t be afraid of the default code that we are provided from <code>Eclipse<\/code>. Regarding an <code>item<\/code>, we actually need to define 3 properties:<\/p>\n<ol>\n<li>An <code>id<\/code>, which is useful to handle the action that we want our menu option to implement, when clicked. This can be done through a Java class.<\/li>\n<li>An <code>icon<\/code>, which has to lie under the <code>drawable<\/code> (we also have to take care about dimensions, as it is a serious objective in Android; for more details, you can refer <a href=\"http:\/\/developer.android.com\/design\/style\/iconography.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>).<\/li>\n<li>A <code>title<\/code>, which is the displayed text in our menu <code>item<\/code>; this is about the <code>title<\/code> attribute we already know: it can be manipulated with both the &#8220;hardcoded&#8221; way (as we &#8216;ll show in this example) and through the stored <code>strings<\/code> of our app.<\/li>\n<\/ol>\n<p>Following the fore-mentioned guidelines, we have everything we need, in order to create a menu whom&#8217;s items have icons attached on them. Well, <b>not exactly<\/b>, please take a look <a href=\"http:\/\/developer.android.com\/guide\/topics\/ui\/menus.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. That is, the above guidelines support our development if only our application targets <b>Android 2.3 or lower<\/b>.<\/p>\n<p>But here, we deal with the latest Android API (that&#8217;s why we &#8216;re getting by default the marked lines in <code>main.xml<\/code>; please consider the following <a href=\"http:\/\/stackoverflow.com\/questions\/17914017\/android-4-3-menu-item-showasaction-always-ignored\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a>, too) and what does this mean, is that the dashed line in the right of our <a href=\"http:\/\/developer.android.com\/guide\/topics\/ui\/actionbar.html\" target=\"_blank\" rel=\"noopener noreferrer\">ActionBar<\/a> represents the latest functionality of a menu; so, on click, its reaction is same as by pressing the hardware &#8220;Menu&#8221; button.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>So, before jumping to the full example, here is the list of tools that we &#8216;ll use:<\/p>\n<p>For the need of this example, we will use the following tools in a Windows 64-bit platform:<br \/>\n&nbsp;<\/p>\n<ol>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.4 Luna<\/li>\n<li>Android SDK 4.4<\/li>\n<\/ol>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<h2>2. Creating a New Android Application Project<\/h2>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p>Fill in the name of the application, the project and the package in the appropriate fields and then click Next.<\/p>\n<p><figure id=\"attachment_12279\" aria-describedby=\"caption-attachment-12279\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project11.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project11.png\" alt=\"Figure 2. Create a new Android application\" width=\"636\" height=\"543\" class=\"size-full wp-image-12279\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project11.png 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project11-300x256.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-12279\" class=\"wp-caption-text\">Figure 2. Create a new Android application<\/figcaption><\/figure><\/p>\n<p>In the next window, the \u201cCreate Activity\u201d option should be checked. The new created activity will be the main activity of your project. Then press Next button.<\/p>\n<p><figure id=\"attachment_12280\" aria-describedby=\"caption-attachment-12280\" style=\"width: 636px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project21.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project21.png\" alt=\"Figure 3. Configure the project\" width=\"636\" height=\"543\" class=\"size-full wp-image-12280\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project21.png 636w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/project21-300x256.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><figcaption id=\"caption-attachment-12280\" class=\"wp-caption-text\">Figure 3. Configure the project<\/figcaption><\/figure><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. We will use the default icon of android, so click Next button.<\/p>\n<p><figure id=\"attachment_12275\" aria-describedby=\"caption-attachment-12275\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_31.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_31.png\" alt=\"Figure 4. Configure the launcher icon\" width=\"660\" height=\"590\" class=\"size-full wp-image-12275\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_31.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_31-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-12275\" class=\"wp-caption-text\">Figure 4. Configure the launcher icon<\/figcaption><\/figure><\/p>\n<p>Select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><figure id=\"attachment_12276\" aria-describedby=\"caption-attachment-12276\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_41.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_41.png\" alt=\"Figure 5. Create the activity and select its type\" width=\"660\" height=\"590\" class=\"size-full wp-image-12276\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_41.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_41-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-12276\" class=\"wp-caption-text\">Figure 5. Create the activity and select its type<\/figcaption><\/figure><\/p>\n<p>You then have to specify a name for the new Activity and a name for the layout description of your app. The .xml files for the layout will automatically be created in the <code>res\/layout<\/code> folder. Finally, press Finish.<\/p>\n<p><figure id=\"attachment_12277\" aria-describedby=\"caption-attachment-12277\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_51.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_51.png\" alt=\"Figure 6. Create a new blank activity\" width=\"660\" height=\"590\" class=\"size-full wp-image-12277\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_51.png 660w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/newproject_51-300x268.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-12277\" class=\"wp-caption-text\">Figure 6. Create a new blank activity<\/figcaption><\/figure><\/p>\n<p>Here is the very final structure of the project, just in case you &#8216;ll miss something, while developing:<\/p>\n<p><figure id=\"attachment_12281\" aria-describedby=\"caption-attachment-12281\" style=\"width: 371px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/final_structure.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/final_structure.png\" alt=\"Figure 7. The final structure of the project\" width=\"371\" height=\"575\" class=\"size-full wp-image-12281\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/final_structure.png 371w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/final_structure-193x300.png 193w\" sizes=\"(max-width: 371px) 100vw, 371px\" \/><\/a><figcaption id=\"caption-attachment-12281\" class=\"wp-caption-text\">Figure 7. The final structure of the project<\/figcaption><\/figure><\/p>\n<h2><span id=\"code\">3. Creating the layout of our Menu<\/span><\/h2>\n<p><i>As we deal with menus in this example, I &#8216;ll just leave the default <code>TextView<\/code> provided\/generated by the IDE, as is.<\/i>[ulp id=&#8217;Ja8Orb5oPKdShcXt&#8217;]<\/p>\n<p>Let&#8217;s do it by opening <code>res\/menu\/main.xml<\/code> and navigating to the respective xml tab and paste the following.<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>main.xml<\/code><\/i><\/span><\/p>\n<pre class=\"brush:xml; highlight: [2, 10, 15]\">&lt;menu xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    tools:context=\"com.javacodegeeks.android.menu.MainActivity\" \n    xmlns:custom=\"http:\/\/schemas.android.com\/apk\/res-auto\"&gt;\n    \n    &lt;item android:id=\"@+id\/about\"\n\t\tandroid:icon=\"@drawable\/about_icon\"\n\t\tandroid:title=\"About\" \n\t\tapp:showAsAction=\"always\"\/&gt;\n    \n\t&lt;item android:id=\"@+id\/help\"\n\t\tandroid:icon=\"@drawable\/help_icon\"\n\t\tandroid:title=\"Help\" \n\t\tapp:showAsAction=\"always\"\/&gt;\n\n&lt;\/menu&gt;\n<\/pre>\n<p>That is, we want both of our menu items to be displayed as icons in the action bar, so we have to configure our menu, by considering the highlighted lines.<\/p>\n<p><i>At this point, a possible question could be relative to the source of our test icons and the quick adjustment of them in our project. To get things easier, I&#8217;m always using <a href=\"http:\/\/www.iconarchive.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a> site, when I have to find an icon\/image for an application that I&#8217;m developing; every resource there is completely free and you have the right to use it wherever you want.<\/i><\/p>\n<p>We also have to provide ids to our menu items, in order to make them accesible through a Java class; i.e. we want to show a <a href=\"http:\/\/developer.android.com\/guide\/topics\/ui\/notifiers\/toasts.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>Toast<\/code><\/a>, each time the user interacts with one of them.<\/p>\n<h2>4. Coding the Main Activity<\/h2>\n<p>Let&#8217;s first navigate to <code>src\/com.javacodegeeks.android.menuapp\/MainActivity.java<\/code>. Next, complete the class like this:<\/p>\n<p><span style=\"text-decoration: underline\"><i><code>MainActivity.java<\/code><\/i><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.android.menuapp;\n\nimport android.os.Bundle;\nimport android.support.v7.app.ActionBarActivity;\nimport android.view.Menu;\nimport android.view.MenuItem;\nimport android.widget.Toast;\n\npublic class MainActivity extends ActionBarActivity {\n\n\t@Override\n\tprotected void onCreate(Bundle savedInstanceState) {\n\t\tsuper.onCreate(savedInstanceState);\n\t\tsetContentView(R.layout.activity_main);\n\t}\n\n\t@Override\n\tpublic boolean onCreateOptionsMenu(Menu menu) {\n\t\t\/\/ Inflate the menu; this adds items to the action bar if it is present.\n\t\tgetMenuInflater().inflate(R.menu.main, menu);\n\n\t\treturn true;\n\t}\n\n\t@Override\n\tpublic boolean onOptionsItemSelected(MenuItem item) {\n\t\t\/\/ Handle action bar item clicks here. The action bar will\n\t\t\/\/ automatically handle clicks on the Home\/Up button, so long\n\t\t\/\/ as you specify a parent activity in AndroidManifest.xml.\n\t\tint id = item.getItemId();\n\t\tswitch (id) {\n\t\tcase R.id.about:\n\t\t\tToast.makeText(getApplicationContext(), \"About menu item pressed\", Toast.LENGTH_SHORT).show();\n\t\t\tbreak;\n\t\tcase R.id.help:\n\t\t\tToast.makeText(getApplicationContext(), \"Help menu item pressed\", Toast.LENGTH_SHORT).show();\n\t\t\tbreak;\n\t\t}\n\t\treturn super.onOptionsItemSelected(item);\n\t}\n}\n<\/pre>\n<p>Here, an <code>ActionBar<\/code> element exists, so, line&#8217;s 20 execution will add the <code>menu.xml<\/code> items to our action bar.<\/p>\n<p>Having set our initial view of our app, we then have to set a listener, in order to make our app interactive in each menu&#8217;s item tap &#8211; a simple message, relative to the item clicked. This can be done by overriding the existing <code>onOptionsItemSelected<\/code> method (you can find more details about the realtionship of it with a Menu, in the &#8220;Handling click events&#8221; section of the <a href=\"http:\/\/developer.android.com\/guide\/topics\/ui\/menus.html\" target=\"_blank\" rel=\"noopener noreferrer\">official documentation<\/a>).<\/p>\n<h2>5. Running the application<\/h2>\n<p><figure id=\"attachment_12287\" aria-describedby=\"caption-attachment-12287\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_initial.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_initial.png\" alt=\"Figure 8. Initial state of the app\" width=\"488\" height=\"470\" class=\"size-full wp-image-12287\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_initial.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_initial-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-12287\" class=\"wp-caption-text\">Figure 8. Initial state of the app<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s first tap the blue button and then the red one!<\/p>\n<p><figure id=\"attachment_12288\" aria-describedby=\"caption-attachment-12288\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_about_pressed.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_about_pressed.png\" alt=\"Figure 9. Tapping the blue icon\" width=\"488\" height=\"470\" class=\"size-full wp-image-12288\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_about_pressed.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_about_pressed-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-12288\" class=\"wp-caption-text\">Figure 9. Tapping the blue icon<\/figcaption><\/figure><br \/>\n<figure id=\"attachment_12289\" aria-describedby=\"caption-attachment-12289\" style=\"width: 488px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_help_pressed.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_help_pressed.png\" alt=\"Figure 10. Tapping the red icon\" width=\"488\" height=\"470\" class=\"size-full wp-image-12289\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_help_pressed.png 488w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/app_help_pressed-300x288.png 300w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/a><figcaption id=\"caption-attachment-12289\" class=\"wp-caption-text\">Figure 10. Tapping the red icon<\/figcaption><\/figure><\/p>\n<h2>6. Download the Eclipse Project<\/h2>\n<p>This was an example of Menu in Android.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here : <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/07\/MenuApp.zip\"><b>MenuApp.zip<\/b><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we &#8216;re gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from other similars, is that we &#8216;re using the latest Android APIs, where Menus are manipulated differently. Having created an Android project, using the latest platform, gives us by &hellip;<\/p>\n","protected":false},"author":12,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[452],"tags":[],"class_list":["post-11422","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-menu"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Menu Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Today we &#039;re gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Menu Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Today we &#039;re gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/toubou.techblog\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-29T08:00:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-24T13:05:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Thodoris Bais\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ThodorisBais\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Thodoris Bais\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\"},\"author\":{\"name\":\"Thodoris Bais\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983\"},\"headline\":\"Android Menu Example\",\"datePublished\":\"2014-07-29T08:00:08+00:00\",\"dateModified\":\"2019-04-24T13:05:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\"},\"wordCount\":1056,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"articleSection\":[\"Menu\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\",\"name\":\"Android Menu Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2014-07-29T08:00:08+00:00\",\"dateModified\":\"2019-04-24T13:05:32+00:00\",\"description\":\"Today we 're gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"view\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/view\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Menu\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/view\/menu\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Android Menu Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983\",\"name\":\"Thodoris Bais\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg\",\"caption\":\"Thodoris Bais\"},\"description\":\"Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics &amp; Telecommunications Engineering and is interested in continuous development.\",\"sameAs\":[\"http:\/\/thodorisbais.blogspot.com\",\"https:\/\/www.facebook.com\/toubou.techblog\/\",\"https:\/\/instagram.com\/thodoris.bais\/\",\"https:\/\/www.linkedin.com\/pub\/thodoris-bais\/69\/6b7\/408\",\"https:\/\/x.com\/@ThodorisBais\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/thodoris-bais\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Menu Example - Java Code Geeks","description":"Today we 're gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/","og_locale":"en_US","og_type":"article","og_title":"Android Menu Example - Java Code Geeks","og_description":"Today we 're gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/toubou.techblog\/","article_published_time":"2014-07-29T08:00:08+00:00","article_modified_time":"2019-04-24T13:05:32+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Thodoris Bais","twitter_card":"summary_large_image","twitter_creator":"@ThodorisBais","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Thodoris Bais","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/"},"author":{"name":"Thodoris Bais","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983"},"headline":"Android Menu Example","datePublished":"2014-07-29T08:00:08+00:00","dateModified":"2019-04-24T13:05:32+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/"},"wordCount":1056,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","articleSection":["Menu"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/","name":"Android Menu Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2014-07-29T08:00:08+00:00","dateModified":"2019-04-24T13:05:32+00:00","description":"Today we 're gonna see how to create a Menu with icon items, which on click, fire a respective to them message. What differentiates this article from","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/view\/menu\/android-menu-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"view","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/view\/"},{"@type":"ListItem","position":5,"name":"Menu","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/view\/menu\/"},{"@type":"ListItem","position":6,"name":"Android Menu Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/045f5a70ed608824c16959f146ede983","name":"Thodoris Bais","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/03\/Thodoris-Bais_avatar_1425256492-96x96.jpg","caption":"Thodoris Bais"},"description":"Thodoris is an Oracle Certified Associate Java Programmer and currently works as a Junior Software Developer, for Intrasoft International S.A. He holds a diploma at Informatics &amp; Telecommunications Engineering and is interested in continuous development.","sameAs":["http:\/\/thodorisbais.blogspot.com","https:\/\/www.facebook.com\/toubou.techblog\/","https:\/\/instagram.com\/thodoris.bais\/","https:\/\/www.linkedin.com\/pub\/thodoris-bais\/69\/6b7\/408","https:\/\/x.com\/@ThodorisBais"],"url":"https:\/\/examples.javacodegeeks.com\/author\/thodoris-bais\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=11422"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/11422\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=11422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=11422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=11422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}