Skip to main content

Options menu


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

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...