..


Sponsored Links

Menu: OptionsMenu Association and a listener

In this lesson we are going to analzizare all'OptionsMenu the code we created in the previous lesson.

The code within the method onCreate, at this point of the guide, should be clearly understood by the reader and therefore it is deliberately omitted the analysis. Instead focus our attention on a new method that we had not yet seen the method OnCreateOptionMenu (Menu menu).

This method is automatically invoked by activity as soon as the user clicks the menu button or the physical device simulator. By default the menu is empty and it is therefore necessary to redefine the method presented above to view a personalized menu. As we can see the method returns a Boolean value: true effect will be to notify the Activity to show the menu, while false has the opposite effect (the menu will not show).

Continuing with the analysis of the code we find the method that lets you add an item to the menu we are creating. The prototype of this method is as follows:

 



 add (int groupId, int itemId, int order, CharSequence title)

 
  • GroupId parameter is a parameter that indicates the group membership of the menu item (in our case we used Menu.NONE because we do not want to use any group);
  • itemId indicates the id to be associated with the menu item (will be useful in the process of identifying which menu item the user clicked);
  • order is used to specify the location of the menu item within the menu itself;
  • title specifies the text string that will be displayed to the user.

As we said you can replace the text with an image of the menu item. In this case, you must import the class anroid.view.MenuItem and replace the code for adding a menu item with the following:






 Voce1 menu.add MenuItem = (Menu.NONE, 1, 1, "voce1");

 





 voce1.setIcon (R.drawable.icon);



As we can see you need to create a MenuItem using the add method presented first few paragraphs and then invoke the method setIcon on this new object.

We associate a listener to each menu item

And 'possible, following the technique already used with the buttons, create an object of type MenuItem, associate a listener, and then override the method that captures the click.
In this guide, however, we will present another method, simpler and easier to implement, which will use the first implementation of the menu (the one without the declaration of the MenuItem) and we will add the method onOptionsItemSelected (MenuItem item) that is the listener that you must use for menus. Now let's see the code to add:






 public boolean onOptionsItemSelected (MenuItem item) {

 

  



 int id = item.getItemId ();

    

  



 switch (id) {



    



 case 1:



      



 textView.setText ("I clicked the" + id + "menu");



      



 break;



    

    



 case 2:



      



 textView.setText ("I clicked the" + id + "menu");



      



 break;



  



 }







 }



For reasons of space we have managed just click on the first two menu items, you leave as an exercise to the reader the completion of the case.
The code is very simple fact is stored in the variable id the ID number associated with the menu item that was clicked. This is done with the call, object item, the method getId ().
Within each case we will insert the code that models the behavior of the application at the click of the menu item that calls another activity usually replacing the current one (we will see later how).

Help develop applications for Android
E-Learning
ASP Zero (Ebook) ASP Zero (Ebook)
Learning Microsoft ASP and VBScript from scratch. At only 29 €.
AutoCAD (eBook) AutoCAD (eBook)
Creation of architectural structures. At only 29 €.
XML (Course) XML (Course)
Creation of XML structures, XSL and other languages ​​extensible. Starting from 29 €.
Sponsored Links