From the course: Android Development Essential Training: App Architecture with Kotlin

Define an options menu with XML - Android Tutorial

From the course: Android Development Essential Training: App Architecture with Kotlin

Start my 1-month free trial

Define an options menu with XML

- [Instructor] Every Android activity has the ability to display an options menu, I've previously described how to create an options menu that displays a button on the toolbar, but you can also create a dropdown menu that's activated by an icon that appears in the top corner of the activity. I'm going to add some code to my application that opens an about activity, that is an activity that shows some extensive text, and to speed things along a little bit, I've added a new activity to this project in this branch of the GitHub repo, it's called AboutActivity and it's very simple, it just has an OnCreate function in the class and then it sets its content view from this layout file, Activity About, this activity screen has a simple text view and it's wrapped inside a component called nested scroll view and this will provide vertical scrolling for very long text. Because this text view is going to display five paragraphs of what's called lorem ipsum text. So let's go back to the main activity, and as in previous exercises, the first step is to create a new menu file, this time I'll go to my existing menu directory under Resources and I'll create a New Menu Resource file. I'll name it menu_main and I'll accept the directory name menu and click okay. Next I'll add a new menu item, I'll give it a title of about. I'll give it an ID of action_about, I don't need an icon for this menu and for showAsAction I'll select never. Because I want it to appear as a drop down menu. Next I'll go to my main activity class, and as I did previously, I'll override the function onCreateOptionsMenu and then I'll inflate the menu file, with menuInflator.inflate I'll pass in the ID of the menu file R.menu.menu_main and then the menu reference that's passed in as an argument to this function. Now when the main activity appears I get the three dots in the top corner and when I touch them I see the menu appear, right now nothing's happening and so I'll go back to Android Studio and make some more changes. Now previously I added code to start an activity from the first activity, but this time I want to add that code in the about activity, just to show you a different way of factoring the code. I'll create a companion object section. So this is the equivalent of a static section in Java, I'll create a new function that I'll simply call start, and here I'll create the code I need to create an intent object. The intent class needs the current context, I'll get that by allowing it to be passed in to this start function, I'll name the argument context and give it a type of context from Android.content. Then I'll pass in that context and then the name of this activity, AboutActivity colon, colon class.Java and then I'll call context.startActivity and I'll pass in the intent. Now I'll go back to the main activity class, I'll override the function that's called from the framework when an item from the options menu is selected. This time I'll use an if statement to evaluate the item, I'll evaluate the expression item question mark .itemId and see if it matches R.id.action_about and if that's true then I'll call AboutActivity.start and I'll pass in this as the context, and I'll return true. So this is just a different way of evaluating the menu item, when you only have one possible menu item you don't need a when statement or expression, and I'll test this code. I'll once again start on my main activity, I'll open the options menu and choose about and the about activity opens, and because this text view is inside a scrolling component I can easily scroll the text up and down, my up button is appearing so I'll press it and I return to the main activity. You can add as many items to an options menu as you like to support your app's navigational requirements and you can choose whether to display these navigational components in the options menu or in a navigation drawer which I'll describe in the next couple of videos.

Contents