{"id":4713,"date":"2013-04-26T15:21:47","date_gmt":"2013-04-26T15:21:47","guid":{"rendered":"https:\/\/www.techrounder.com\/blog\/create-android-native-menu-in-phonegap\/"},"modified":"2026-01-08T06:02:53","modified_gmt":"2026-01-08T06:02:53","slug":"create-android-native-menu-in-phonegap","status":"publish","type":"post","link":"https:\/\/www.techrounder.com\/blog\/create-android-native-menu-in-phonegap\/","title":{"rendered":"Create Android Native Menu in Phonegap"},"content":{"rendered":"<p>Here is the step to create native android menu in phonegap.<\/p>\n<p>Step 1 : Add the code in your activity class file<br \/>\n<code>public boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\t\t\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\n\t\tgetMenuInflater().inflate(R.menu.activity_main, menu);<br \/>\n\t\treturn true;<br \/>\n\t}<\/code><\/p>\n<p>It looks like as follows<br \/>\n<code>public class MainActivity extends DroidGap {<br \/>\n\t@Override<br \/>\n\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\tsuper.loadUrl(\"file:\/\/\/android_asset\/www\/index.html\");<br \/>\n\t}<br \/>\n\t@Override<br \/>\n\tpublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\t\t\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\n\t\tgetMenuInflater().inflate(R.menu.activity_main, menu);<br \/>\n\t\treturn true;<br \/>\n\t}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Step 2 : Now create an <strong>activity_main.xml<\/strong> file in <strong>&#8220;res\/menu\/&#8221;<\/strong> folder<\/p>\n<p>Add the following code in it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techrounder.com\/blog\/assets\/uploads\/2013\/04\/code1.jpg\" alt=\"\" width=\"100%\" class=\"alignleft size-full wp-image-4409\" \/><\/p>\n<p>Step 3 : Now define the strings in the strings.xml file at <strong>&#8220;res\/values\/&#8221;<\/strong> location<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techrounder.com\/blog\/assets\/uploads\/2013\/04\/code2.jpg\" alt=\"\" width=\"100%\" class=\"alignleft size-full wp-image-4412\" \/><\/p>\n<p>Step 4 : Also define the ids and string in R.java file at <strong>&#8220;gen\/com.example.testapp\/&#8221;<\/strong>, normally the ids and all other variables will be automatically added to R.java file, so you only needed to add it manually if shows error.  <\/p>\n<p><code><br \/>\npublic final class R {<br \/>\n    public static final class attr {<br \/>\n    }<br \/>\n    public static final class drawable {<br \/>\n        public static final int ic_launcher=0x7f020000;<br \/>\n    }<br \/>\n    public static final class id {<br \/>\n        public static final int menu_exit=0x7f080003;<br \/>\n        public static final int menu_home=0x7f080001;<br \/>\n        public static final int menu_settings=0x7f080002;<br \/>\n    }<br \/>\n    public static final class layout {<br \/>\n        public static final int activity_main=0x7f030000;<br \/>\n    }<br \/>\n    public static final class menu {<br \/>\n        public static final int activity_main=0x7f070000;<br \/>\n    }<br \/>\n    public static final class string {<br \/>\n        public static final int app_name=0x7f050000;<br \/>\n        public static final int hello_world=0x7f050001;<br \/>\n        public static final int menu_exit=0x7f050005;<br \/>\n        public static final int menu_home=0x7f050004;<br \/>\n        public static final int menu_settings=0x7f050002;<\/p>\n<p>        public static final int really_quit=0x7f050007;<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Step 5: Now add code in your main activity so that the menu action to be done when clicking them.<\/p>\n<p><code><br \/>\n@Override<br \/>\n    public boolean onOptionsItemSelected(MenuItem item) {<br \/>\n        \/\/ Handle item selection<br \/>\n        switch (item.getItemId()) {<br \/>\n            case R.id.menu_home:<br \/>\n            \/\/\/\/code to be executed<br \/>\n                return true;<br \/>\n            case R.id.menu_settings:<br \/>\n             \/\/\/\/code to be executed<br \/>\n            \treturn true;<br \/>\n            case R.id.menu_exit:<br \/>\n              \/\/\/\/code to be executed<br \/>\n                return true;<br \/>\n            default:<br \/>\n                return super.onOptionsItemSelected(item);<br \/>\n        }<br \/>\n    }<br \/>\n<\/code><\/p>\n<p>Now it looks like this<\/p>\n<p><code><br \/>\npublic class MainActivity extends DroidGap {<br \/>\n\t@Override<br \/>\n\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t\tsuper.loadUrl(\"file:\/\/\/android_asset\/www\/index.html\");<br \/>\n\t}<br \/>\n\t@Override<br \/>\n\tpublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\t\t\/\/ Inflate the menu; this adds items to the action bar if it is present.<br \/>\n\t\tgetMenuInflater().inflate(R.menu.activity_main, menu);<br \/>\n\t\treturn true;<br \/>\n\t}<\/p>\n<p>        public boolean onOptionsItemSelected(MenuItem item) {<br \/>\n                \/\/ Handle item selection<br \/>\n                switch (item.getItemId()) {<br \/>\n                   case R.id.menu_home:<br \/>\n                     \/\/\/\/code to be executed<br \/>\n                     return true;<br \/>\n                   case R.id.menu_settings:<br \/>\n                    \/\/\/\/code to be executed<br \/>\n            \t    return true;<br \/>\n                   case R.id.menu_exit:<br \/>\n                    \/\/\/\/code to be executed<br \/>\n                    return true;<br \/>\n                   default:<br \/>\n                    return super.onOptionsItemSelected(item);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Now it works !!!<\/p>\n","protected":false},"excerpt":{"rendered":"Here is the step to create native android menu in phonegap. Step 1 : Add the code in&hellip;","protected":false},"author":2,"featured_media":244,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"csco_post_video_bg_volume":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-4713","post","type-post","status-publish","format-standard","has-post-thumbnail","category-phonegap","cs-entry","cs-video-wrap"],"_links":{"self":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/comments?post=4713"}],"version-history":[{"count":0,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/posts\/4713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media\/244"}],"wp:attachment":[{"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/media?parent=4713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/categories?post=4713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techrounder.com\/blog\/wp-json\/wp\/v2\/tags?post=4713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}