..


Sponsored Links

Menu: The ContextMenu

In previous chapters we have analyzed in detail the type menu IconMenu which are generally the most used. Another resource that Android offers us is the ContextMenu that can be lumped together as the structure and operation, the menu that appears when you click the right mouse button on any Windows icon.

The ContextMenu fact is not shown to the user after a push of a button on the device (as it was for IconMenu), but is associated with a particular widget is displayed to the user, and after a prolonged right-click on this widget.

Obviously, within an activity, you can create ContextMenu and then associate them with various different widgets, or create a single ContextMenu is associated with multiple different widgets.

We are now going to show the code needed to create two different and then associate a ContextMenu with a button and another to a TextView (before you enter the code you need to import the package android.view.ContextMenu .*):






 public void onCreate (Bundle savedInstanceState) {



  



 super.onCreate (savedInstanceState);

  

  



 LinearLayout mioLayout LinearLayout = new (this);

 

  

  



 Button button = new Button (this);

 

  



 bottone.setId (1);



  



 button1.setText ("associated with a menu button");

 

        

  



 TextView tv = new TextView (this);



  



 tv.setText ("Give a click for prolonged contextmenu");



  



 tv.setId (2);



  

  



 mioLayout.addView (button);



  



 mioLayout.addView (TV);



  



 setContentView (layout);



  

  



 registerForContextMenu (button);

 

  



 registerForContextMenu (TV);







 }









 public void onCreateContextMenu (ContextMenu ContextMenu, View view, ContextMenuInfo menuInfo) {



  



 if (view.getId () == 1) {



    



 menu.setHeaderTitle ("the ContextMenu button");



    



 menu.add (Menu.NONE, 1, 1, "Voice button 1");



    



 menu.add (Menu.NONE, 2, 2, "Voice button 2");



  



 }



  



 else if (view.getId () == 2) {



    



 menu.setHeaderTitle ("ContextMenu of TextView");



    



 menu.add (Menu.NONE, 3, 1, "Item 1 TextView");



    



 menu.add (Menu.NONE, 4, 2, "Item 2 TextView");



  



 }







 }



We analyze the proposed code. As for the content of the method onCreate the only lines of code ever seen are those relating to the invocation of the method that combines registerForContextMenu, passed as a parameter to the widget, any ContextMenu.

Of greater interest is that onCreateContextMenu method instead of fate, contains the code for the ContextMenu that we want to create. In this case, to create a different menu for the two widgets, we used the method getId () of the view that invoked the method onCreateContextMenu (setting the id of the two widgets is performed using the method onCreate setId ()).

If we go inside to add to the menu items we want with the usual method using the shell and add the method to set a title setHeaderTitle ContextMenu.

In order to associate a specific behavior at a pressure of one of the buttons on the menu you are working in the same manner seen in the previous chapter, however, for OptionMenu entering the desired code within the method onContextItemSelected (MenuItem item).

Help develop applications for Android
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
HTML (Course) HTML (Course)
The markup language for the Web from 29 €.
Webmaster Advanced (Course) Webmaster Advanced (Course)
Become a professional Webmaster. From 39 €.
Sponsored Links