..


Sponsored Links

Add buttons to AlertDialog

Obviously a AlertDialog structured like the previous chapter is not very useful because it has no button and actually blocks the application because there is no way to remove it from the screen.
Before adding the buttons all'AlertDialog, however, you must add a TextView object in which we will write a different message depending on the button clicked by the user. The addition of the TextView is left as an exercise to the reader.

For the addition of buttons and the association in ALertDialog listeners simply replace the code written in the previous chapter with the following:






 AlertDialog.Builder miaAlert AlertDialog.Builder = new (this);







 miaAlert.setMessage ("verify the operation of the buttons added!");







 miaAlert.setTitle ("AlertDialog of MrWebMaster");









 miaAlert.setCancelable (false);







 miaAlert.setPositiveButton ("Yes", new DialogInterface.OnClickListener () {



  



 public void onClick (DialogInterface dialog, int id) {



    



 tv.setText ("I clicked the button YES");



  



 }







 });



    	





 miaAlert.setNegativeButton ("No", new DialogInterface.OnClickListener () {



  



 public void onClick (DialogInterface dialog, int id) {



    



 tv.setText ("I clicked the NO button");

   		

  



 }







 });







 AlertDialog miaAlert.create alert = ();







 Alert.show ();



As regards the first three lines of code do not need to explain anything inquanto are identical to those shown in the previous chapter. Going forward with the analysis of the code we find the method invocation setCanceble (false), which has the effect of disabling the back button of the phone and then you will need to click on one of two buttons to close the AlertDialog.

Having said that we pass to the creation of real buttons. The first button is created with the method setPositiveButton which takes as input a string that will be displayed on the screen as the content of the button and a listener. As we can see the listener is declared as a new instance of DialogInterface.OnClickListener and not as a new instance of View.OnClickListener. This fact can only be applied to elements eriditano the View class, and since the AlertDialog not inherit this class, but they are part of the package android.app, you must use a listener like DialogInterface.

In practice, this difference is not sostanzianziale, in fact, in this case, you will need to override the OnClick method, and insert the code for the behavior we want to make two buttons. As we can see the OnClick method takes as input two parameters: the first is an object of type AlertDialog (dialog that tells you what caused the activation of the listener) and then an ID that identifies the button pressed. Just go inside the OnClick method to change the text on the TextView tv.

The structure of the OnClick method suggests an optimized version of our listeners. In fact AlertDialog knowing what triggered the listener and which button was clicked in the alert, you can create a single listener to handle a more AlertDialog and the related buttons. This is accomplished simply by executing a first switch on the attribute Dialog (AlertDialog let's identify what triggered the listener) and in each case on AlertDialog put in another switch to figure out which button on the AlertDialog taken into consideration, it is clicked.

It is left as an exercise to the reader that this new implementation of the listener is basically very similar to that seen in previous chapters of this guide.

Help develop applications for Android
E-Learning
AutoCAD (eBook) AutoCAD (eBook)
Creation of architectural structures. At only 29 €.
Burning CDs and DVDs (Ebook) Burning CDs and DVDs (Ebook)
Guide to burn CDs and DVDs with freeware programs. Just 25 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links