{"id":968,"date":"2012-02-14T16:51:49","date_gmt":"2012-02-14T11:21:49","guid":{"rendered":"http:\/\/www.itechcode.com\/?p=968"},"modified":"2012-12-14T23:13:22","modified_gmt":"2012-12-14T17:43:22","slug":"create-a-menu-android-programming","status":"publish","type":"post","link":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/","title":{"rendered":"Create A Menu : Android Programming"},"content":{"rendered":"<p>In the previous post i have explain you about how to create a simple button for android devices and if you missed that then read it:<\/p>\n<p><strong>Read:\u00a0<a rel=\"nofollow\" href=\"http:\/\/wp.me\/p2587B-ch\" target=\"_blank\">How to Create a Simple Button Control &#8211; Android Programming<\/a><\/strong><\/p>\n<p>Now moving ahead to this and today we are going to program one of the used feature called <strong>Menus <\/strong>which you saw used it in many apps and this control gives the user to better navigation power. Android also offers a syntax and keywords which is available in Android SDK and its documentation.<\/p>\n<p>Now to create a Menu in android here is the syntax:<\/p>\n<p><strong>menu.add(groupId, itemId, order, titleRes);<\/strong><\/p>\n<ul>\n<li>groupid: Your app may have more than one menus then group menus can be used to\u00a0separate\u00a0the common menu items.(0,1..n).<\/li>\n<li>ItemId: Each content in menu is considered as itemId. Give value like 1 ,2,3 for indexing.<\/li>\n<li>order: Which menu to display first and then second. Default is order of creation but you can change it.<\/li>\n<li>titleres: Title of Menu or caption.<\/li>\n<\/ul>\n<p>Now to create a menu we have to use a constructor\u00a0<strong>onCreateOptionsMenu(Menu var1) <\/strong>which is available in Android SDK. Here refer the code for it.<\/p>\n<p><strong>public boolean onCreateOptionsMenu(Menu menu)<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>menu.add(0, Option1, 1, &#8220;Test 2&#8221;).setIcon(R.drawable.menu_add);<\/strong><\/p>\n<p><strong>menu.add(0, Option2, 2, &#8220;Test 1&#8221;).setIcon(R.drawable.menu_edit);<\/strong><\/p>\n<p><strong>menu.add(0, Option3, 3, &#8220;Test 3&#8221;);<\/strong><\/p>\n<p><strong>menu.add(0, Option4, 4, &#8220;Test 4&#8221;);<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>Here i pass the item id a declared constant variable. Like this:<\/p>\n<p><strong>public static final int Option1 = 1;<\/strong><\/p>\n<p><strong>public static final int Option2 \u00a0= 2;<\/strong><\/p>\n<p><strong>public static final int Option3 = 3;<\/strong><\/p>\n<p><strong>public static final int Option4 = 4;<\/strong><\/p>\n<p>but you can pass the numeric value directly like 1,2,3,4 but for better programming practice this is the standard way.<\/p>\n<p>If you noticed then i used\u00a0<strong>setIcon(R.drawable.menu_add); <\/strong>in the above code. This code is responsible for placing image with the menu where <strong>R.Drawable<\/strong> is the static function which we used to create the image.<\/p>\n<p><a href=\"http:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/02\/androidmenu.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-969\" title=\"androidmenu\" src=\"http:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/02\/androidmenu.png\" alt=\"\" width=\"313\" height=\"134\" srcset=\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/02\/androidmenu.png 313w, https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/02\/androidmenu-300x128.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/a><\/p>\n<p>This is the sample output by assuming that you know how to run this and if not then read our post for android basics.<\/p>\n<p>Thank you and Happy programming.<\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In the previous post i have explain you about how to create a simple button for android devices and if you missed that then read it: Read:\u00a0How to Create a Simple Button Control &#8211; Android Programming Now moving ahead to this and today we are going to program one of the used feature called Menus [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[98],"class_list":["post-968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-home","tag-android-programming","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Create A Menu : Android Programming<\/title>\n<meta name=\"description\" content=\"How to create a Menu in android App.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create A Menu : Android Programming\" \/>\n<meta property=\"og:description\" content=\"How to create a Menu in android App.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"iTechCode\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-14T11:21:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-12-14T17:43:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shahid\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shahid\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\"},\"author\":{\"name\":\"Shahid\",\"@id\":\"https:\/\/www.itechcode.com\/#\/schema\/person\/c5a1f687774fe3271cbc06796c479fc1\"},\"headline\":\"Create A Menu : Android Programming\",\"datePublished\":\"2012-02-14T11:21:49+00:00\",\"dateModified\":\"2012-12-14T17:43:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\"},\"wordCount\":349,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/www.itechcode.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png\",\"keywords\":[\"Android Programming\"],\"articleSection\":[\"Home\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\",\"url\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\",\"name\":\"Create A Menu : Android Programming\",\"isPartOf\":{\"@id\":\"https:\/\/www.itechcode.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png\",\"datePublished\":\"2012-02-14T11:21:49+00:00\",\"dateModified\":\"2012-12-14T17:43:22+00:00\",\"description\":\"How to create a Menu in android App.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage\",\"url\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png\",\"contentUrl\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png\",\"width\":500,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.itechcode.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create A Menu : Android Programming\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.itechcode.com\/#website\",\"url\":\"https:\/\/www.itechcode.com\/\",\"name\":\"iTechCode\",\"description\":\"An Eye View Of Technology\",\"publisher\":{\"@id\":\"https:\/\/www.itechcode.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.itechcode.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.itechcode.com\/#organization\",\"name\":\"iTechCode\",\"url\":\"https:\/\/www.itechcode.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.itechcode.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2019\/07\/final-logo.png\",\"contentUrl\":\"https:\/\/www.itechcode.com\/wp-content\/uploads\/2019\/07\/final-logo.png\",\"width\":350,\"height\":100,\"caption\":\"iTechCode\"},\"image\":{\"@id\":\"https:\/\/www.itechcode.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.itechcode.com\/#\/schema\/person\/c5a1f687774fe3271cbc06796c479fc1\",\"name\":\"Shahid\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g\",\"caption\":\"Shahid\"},\"description\":\"Shahid is a Engineering student and addicted to Technology. He is Working For iTechCode(Editor-In-Chief) very passionate about blogging and technology. His area of interest includes programming, tech gadgets, gaming, internet marketing, blogging etc.\",\"sameAs\":[\"http:\/\/www.itechcode.com\"],\"url\":\"https:\/\/www.itechcode.com\/author\/shaikh-shahid\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create A Menu : Android Programming","description":"How to create a Menu in android App.","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:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/","og_locale":"en_US","og_type":"article","og_title":"Create A Menu : Android Programming","og_description":"How to create a Menu in android App.","og_url":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/","og_site_name":"iTechCode","article_published_time":"2012-02-14T11:21:49+00:00","article_modified_time":"2012-12-14T17:43:22+00:00","og_image":[{"width":500,"height":500,"url":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png","type":"image\/png"}],"author":"Shahid","twitter_misc":{"Written by":"Shahid","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#article","isPartOf":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/"},"author":{"name":"Shahid","@id":"https:\/\/www.itechcode.com\/#\/schema\/person\/c5a1f687774fe3271cbc06796c479fc1"},"headline":"Create A Menu : Android Programming","datePublished":"2012-02-14T11:21:49+00:00","dateModified":"2012-12-14T17:43:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/"},"wordCount":349,"commentCount":2,"publisher":{"@id":"https:\/\/www.itechcode.com\/#organization"},"image":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png","keywords":["Android Programming"],"articleSection":["Home"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/","url":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/","name":"Create A Menu : Android Programming","isPartOf":{"@id":"https:\/\/www.itechcode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage"},"image":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png","datePublished":"2012-02-14T11:21:49+00:00","dateModified":"2012-12-14T17:43:22+00:00","description":"How to create a Menu in android App.","breadcrumb":{"@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#primaryimage","url":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png","contentUrl":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2012\/01\/android-logo.png","width":500,"height":500},{"@type":"BreadcrumbList","@id":"https:\/\/www.itechcode.com\/2012\/02\/14\/create-a-menu-android-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.itechcode.com\/"},{"@type":"ListItem","position":2,"name":"Create A Menu : Android Programming"}]},{"@type":"WebSite","@id":"https:\/\/www.itechcode.com\/#website","url":"https:\/\/www.itechcode.com\/","name":"iTechCode","description":"An Eye View Of Technology","publisher":{"@id":"https:\/\/www.itechcode.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.itechcode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.itechcode.com\/#organization","name":"iTechCode","url":"https:\/\/www.itechcode.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.itechcode.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2019\/07\/final-logo.png","contentUrl":"https:\/\/www.itechcode.com\/wp-content\/uploads\/2019\/07\/final-logo.png","width":350,"height":100,"caption":"iTechCode"},"image":{"@id":"https:\/\/www.itechcode.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.itechcode.com\/#\/schema\/person\/c5a1f687774fe3271cbc06796c479fc1","name":"Shahid","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b1b4d2abd4f9a6b1b636e183582fafa93bfac5291c6d6b96de404c7f2484711d?s=96&r=g","caption":"Shahid"},"description":"Shahid is a Engineering student and addicted to Technology. He is Working For iTechCode(Editor-In-Chief) very passionate about blogging and technology. His area of interest includes programming, tech gadgets, gaming, internet marketing, blogging etc.","sameAs":["http:\/\/www.itechcode.com"],"url":"https:\/\/www.itechcode.com\/author\/shaikh-shahid\/"}]}},"_links":{"self":[{"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":0,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/media\/484"}],"wp:attachment":[{"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itechcode.com\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}