{"id":74,"date":"2011-02-02T08:00:37","date_gmt":"2011-02-02T07:00:37","guid":{"rendered":"http:\/\/www.mustdev.com\/?p=74"},"modified":"2021-03-29T11:51:40","modified_gmt":"2021-03-29T10:51:40","slug":"android-custom-menu","status":"publish","type":"post","link":"https:\/\/www.mustdev.com\/en\/android-custom-menu\/","title":{"rendered":"Android custom menu"},"content":{"rendered":"<div>\n<p>Menus are very important in Android application because they offer an easy way to access application functions and settings. The menus are binded with the current activity. The menu raise when it pressed the menu button on your device.<br \/>\nTo create a custom menu for our application, we follow these simply steps:<\/p>\n<ul>\n<li>Define the menu and its items<\/li>\n<li>Handling the event menu button of the device<\/li>\n<li>Manage a selected item<\/li>\n<\/ul>\n<h4><!--more--><img title=\"More...\" src=\"http:\/\/www.mustdev.com\/wp-includes\/js\/tinymce\/plugins\/wordpress\/img\/trans.gif\" alt=\"\">DEFINE THE MENU<\/h4>\n<p>Define the menu is a very simple step. We just create a xml file in \/res\/menu\/ directory of the our project like this:<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;menu xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\n    &lt;item android:id=\"@+id\/setting\"\n          android:icon=\"@drawable\/ic_setting\"\n          android:title=\"@string\/setting\" \/&gt;\n    &lt;item android:id=\"@+id\/quit\"\n          android:icon=\"@drawable\/ic_quit\"\n          android:title=\"@string\/quit\" \/&gt;\n&lt;\/menu&gt;<\/code><\/pre>\n<p>The xml above is composed by two main parts (or two tags):<\/p>\n<ul>\n<li>The menu tag<\/li>\n<li>The item tag<\/li>\n<\/ul>\n<p>The menu tag is the root tag of the file. We just past and copy into our xml.<br \/>\nThe item tag describe our menu button. It&#8217;s formed by three property:<\/p>\n<ul>\n<li>android:id.&nbsp;&nbsp;It is a unique id that we want assign to the button and we will use to refer it into the code.<\/li>\n<li>android:icon. &nbsp;It is optional and it is add an icon to the button. The icon must be placed into \/res\/drawable\/ directory. For more info about menu icons go&nbsp;<a title=\"Android menu icons guideline\" href=\"http:\/\/developer.android.com\/guide\/practices\/ui_guidelines\/icon_design_menu.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/li>\n<li>android:title. The string that is showed into the menu button.<\/li>\n<\/ul>\n<h4>HANDLING THE EVENT MENU BUTTON<\/h4>\n<p>When we press the menu button on the device, the onCreateOptionsMenu method is triggered.<br \/>\nWe just add this method on the Activity:<\/p>\n<pre><code>@Override\npublic boolean onCreateOptionsMenu(Menu menu)\n{\n\tMenuInflater inflater = getMenuInflater();\n\tinflater.inflate(R.menu.setting_menu, menu);\n\treturn true;\n}\n<\/code><\/pre>\n<p>The getMenuInflater() method returns a MenuInflater for the Activity.<br \/>\nWith this object, you can call inflate(), which put a menu resource into a Menu object.<br \/>\nIn this example, the menu resource defined by setting_menu.xml is putted into the Menu that was passed into onCreateOptionsMenu().<\/p>\n<h4>MANAGE A SELECTED ITEM<\/h4>\n<p>When the user select an item from the menu, the method onOptionsItemSelected() is called from the Activity. This method passes the MenuItem that the user selected. We can identify the menu item just calling getItem() method of the MenuItem object. This method return the unique id defined by android:id property into the xml file. We use this id to perform appropriate action.<br \/>\nBelow there is a small example:<\/p>\n<pre><code>@Override\npublic boolean onOptionsItemSelected(MenuItem item)\n{\n\tswitch (item.getItemId())\n\t{\n\t\tcase R.id.setting:\n\t\t\tdoSomething();\n\t\t\treturn true;\n\t\tcase R.id.quit:\n\t\t\tfinish();\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn super.onOptionsItemSelected(item);\n\t}\n}\n<\/code><\/pre>\n<p>That&#8217;s all.<br \/>\nWe have done a simple menu.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Menus are very important in Android application because they offer an easy way to access application functions and settings. The menus are binded with the current activity. The menu raise when it pressed the menu button on your device. To create a custom menu for our application, we follow these simply steps: Define the menu\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.mustdev.com\/en\/android-custom-menu\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[76,71],"tags":[65,80],"_links":{"self":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/74"}],"collection":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":6,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/74\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}