Goal of this Lesson
+
In your Activity if you wish to see the options menu then+
- First make sure that your activity extends AppCompatActivity.
- There should be a directory called menu in res directory of your project. If not then create it by right clicking on res director and choose “Android resource directory”. In the new window choose resource type as menu and leave the directory name to menu ( you can change it if you wish to) and press OK.
- Right click on res>>menu directory and choose “Menu resource file”. In the new windows give the file name as you want it. Since I am trying to create an Options Menu for ActivityNavigator activity so I will give a meaningful name as “activity_navigator_menu”. This action will create a xml file in the menu directory.
- You can use the UI to add menu items, remove items, rearrange them and more. I suggest you to fiddle around to get hold on it. Experimenting this the best way to learn :-). You can also just go to text and write the xml code to add the menus, and this is how you should be learning so that you can be aware of the very foundation of the concept. Make sure that you provide id for all the items you add so that it becomes easy for us in coming lesson to detect which menu item has been selected. Suppose if you want to have 3 items (Settings, Help & About ) in your menu then your xml should look like below code sniffet:
As you can see in the above code I have also added the icon to the menu items which will be visible on Action Bar if you set showAsAction attribute for a menu item. For example suppose if you wish to show the settings menu item on Action bar then you can modify the above code as below:
Important Note : You can access the code of MyApp at https://github.com/Felight/Android-Projects >> MyApp. - Once the xml is ready, we should override onCreateOptionsMenu() and inflate the menu we created in this method as shown below:
I am doing it in my ActivityNavigator class which you can do it in any of your activity where you want to create the menu. In the above code the inflate() method of MenuInflater read the activity_navigator_menu.xml and creates instance of type Menu with the items specified in xml and assign the instance to the second argument (menu). Note: onCreateOptionsMenu(Menu menu) method is called the moment user clicks on options button ( right corner of the device ) and obviously the code in this method gets executed.
Handling Option menu selections
- In order detect the menu item selection event and perform some operations. To do that we need to override onOptionsItemSelected(MenuItem item) method which will be called automatically when user click on any menu items. Everything is an Object so the menu item you clicked is itself is an Object which will be sent as input to onOptionsItemSelected() method. So using this object we can compare their id or title and perform some operations specific to menu items selection. I have written the code to go to respective activity upon selecting the menu items. Note : I created HelpActivity, SettingsActivity & AboutActivity in my project and in below code upon selecting the menu items I just used intent to navigate to respective activities.
Code snippet:
+
+
+
Creating Submenus
To add Submenu, just add <menu> items </menu> within an item. For example, suppose if I want to add a new menu item called Support within that if I wish to add two more items like forums & online then I would modify the “activity_navigator_menu.xml” file as below:+
Since I got extra items ( Support submenu ) in menu now I need to handle their selection too. This time I will simply toast their title + ” selected”. So my onOptionsItemSelected() method goes through below modification:+
+
Adding Menu Items (Creating Menu ) in Run time
Creating menu using XML is the best way to create menus, we already dealt with the reasons. Whatever we can do in in XML can also be achieved through programming but reverse is not always true.+
So let’s say we need to create menus at run time then we can simple add the items to the menu object of onCreateOptionsMenu(Menu menu) method. I did mentioned earlier that when you invoke Options menu even though action bar the DVM or ART will creates an instance of Menu and calls onCreateOptions() method where it will pass the instance of Menu it created as argument. Now we can simple use this instance and add the items we want.+
Below the update code of onCreateOptionsMenu() method.+
We will not be using the above code. It’s only for your understanding that items can be added to menu object directly.
Comments
Post a Comment