‫Welcome back!
‫We learned about the drawable layout, Mipmap values, and Xaml folders inside the resources folder.
‫The resources folder contains many folders and many assets.
‫We can create a new Android resource directory.
‫For example, in this video I'm going to create a menu.
‫So there are a lot of resource types you can create animation, color, animator font, drawable layout
‫and others.
‫I'll select menu and keep the directory name as it.
‫Click okay.
‫And inside this menu folder I'll create a new menu resource file.
‫I'll name it as my own menu.
‫Click okay and here we go.
‫This is our menu file in Android Studio.
‫The menu folder is a resource folder that holds Xaml files.
‫Defining menu structures for your Android application.
‫The menu folder is typically located within the resources directory of your Android project menu.
‫Resources define the options and actions available in your apps, action bar or toolbar, or in contextual
‫menus.
‫Let's discuss the menu dot XML file the menu element.
‫The root element menu defines the entire menu.
‫It contains one or more item elements representing individual items.
‫In the menu I'll create two items.
‫I'll define the ID, I'll give it as New note icon.
‫I can specify the icons so Mipmap launcher round.
‫For example, I can set the title new note also, I can use the resources folder that we learned before,
‫which is the strings dot XML.
‫So I'll define string, give it a name new note and give it the value new note.
‫Also I can define another string save note.
‫Let's go to my own menu dot XML.
‫I can use the strings resources by specifying string tag slash new note.
‫And you can see I'm getting the new note text from the Strings.xml.
‫I can define another item in the same way ID save note icon.
‫You can give it whatever you want and the title will be from string dot save.
‫Note here I'm creating a menu with two items.
‫The item element represents an option or action in the menu.
‫Attributes such as ID, icon, title, show as action are commonly used to define the items ID, icon,
‫text and how it should be displayed.
‫Show as action attribute determines how an item is displayed in the action bar or toolbar.
‫The value if room indicates that the item should be shown in the action bar if there is room.
‫Now let's go and inflate this menu in our application.
‫So open your activity which is the Kotlin file.
‫And here I'll override a method called Oncreate options menu type on Create Options menu.
‫And I'm overriding this method on Create Options menu.
‫Method is part of the Appcompat activity lifecycle.
‫It's used to inflate the menu resource associated with an activity.
‫This method is called when the activity is initially created and the menu needs to be displayed in the
‫action bar or toolbar, so this method is used to inflate to display the menu resource.
‫I'll remove this and I'm using the menu inflator dot inflate AR dot menu.
‫This is the resource folder a resource are menu folder dot my own menu value as menu.
‫The menu inflator is an instance of menu inflator class provided by the Android framework.
‫It's used to instantiate menu objects from XML and inflate method is method called on the menu inflator.
‫It takes two parameters.
‫The first one which is the reference to XML resource file.
‫My own menu that we created before that defines the menu structure and the menu.
‫This menu object passed as a parameter to the Oncreate options.
‫New method that inflated menu items will be added to this object.
‫And here I need to specify a return value.
‫The Oncreate options menu should return a boolean value.
‫It indicates whether the menu should be displayed.
‫Returning true indicates that the menu should be displayed, and returning false would prevent it from
‫being displayed.
‫Here I need to return true because I need to display this menu.
‫If we run the program now, we can see there are no menus displayed in my application because this theme
‫prevents us from displaying menus.
‫If we go back to the resources and go back to the themes, you can see the parent theme from which I
‫inherit my own style is inheriting from no action bar.
‫I can remove it.
‫You can use action bar or you can remove it.
‫Okay.
‫Remove it.
‫Let's run again.
‫And here we go.
‫This is our action bar.
‫If we go to here let's click on the three dots.
‫And you can see our menu is inflated and displayed with two items new note and save note.
‫Also you can handle the click events on these items.
‫So if we go back here I'll inherit and override a new method called on options item selected.
‫This method will handle actions when menu items are selected on options, item selected is overridden
‫to handle different menu item selections based on their IDs.
‫Here I should specify the ID of the selected item if item equal equal to r.id dot new note or dot equals.
‫Because if we go back my own menu here, the value of it is a string.
‫So I need to use equals method to compare the item that it is selected with the id.
‫If it is equal to this id, then I'll display a toast message to the user telling him that you select
‫a new note.
‫Now let's run again.
‫This is our menu.
‫If I select New note, nothing happened because I didn't compare the item which is a menu item.
‫I need to get the item ID here.
‫I'm getting the item ID from the item, the selected item, and I'm comparing it with the r.id dot note
‫run again, click on New Note and you can see you select a new note again guys click on New note and
‫here we go.
‫This is how we handle click events on the menu items and how to create a new item in your Android applications.
‫And by the way you can compare these items using the equals sign because these are integers.
‫So thanks for your watching.
‫We learned how to create menus and how to handle the click events on these menus.