..
Another tool commonly used in many applications is the 'AlertDialog. This element is used to display an informational message to the user, typically an error, an unwanted behavior or a confirmation from the user to some action (it is thus able to receive and manage the inputs on the part of ' user). To be clear is that pop-up window that appears, with a little animation, replacing the activity at that time was displayed on the screen.
For example, an application that requires an Internet connection to do some operazoni, un'AlertDialog might show the user when the device you are unable to connect to a Wi-Fi.
It should be noted that the AlertDialog, once displayed on the screen, make the activity that generated without user interaction. It may again interact with the activity only and exclusively when you decide to close the AlertDialog.
Let us now see how to build a custom AlertDialog in the title and message, and having two buttons which will be associated with a listener that will be able to understand which of the two buttons is clicked.
First let's see the code for the creation of a ALertDialog. To do this we can safely position ourselves in a file. Java (if we use the old project file posizioniamoci HelloWorld.java) and the first thing we import the following packages:
android.app.AlertDialog imports; android.content.DialogInterface imports;The first package will import the class relative all'AlertDialog and the second package will import the necessary tools to implement a listener for the buttons to add AlertDialog.
Imported instruments must enter the following code in the OnCreate method, and of course after the invocation of setContentView:
AlertDialog.Builder miaAlert AlertDialog.Builder = new (this);
miaAlert.setTitle ("AlertDialog of MrWebMaster");
miaAlert.setMessage ("This is my first AlertDialog");
AlertDialog miaAlert.create alert = ();
Alert.show ();
Analyzing the code we see that the creation of the entire structure of the alert is assigned to the object AlertDialog.Builder that, through the invocation of the method setTitle setMessage, and sets the title and the message of AlertDialog to display to the user. Note that the constructor of the object is passed as AlertDialog.Builder this context that the activity we are working with right now.
Structured the AlertDialog AlertDialog initializing an object is created with the characteristics set by the builder through the method onCreate. So far we have only instantiated object AlertDialog launched the application and if not we will see on the screen. To avoid this, you invoke the show method, which precisely allows the display screen dell'AlertDialog.
A very common practice is to add, before the title of 'explanatory AlertDialog image should depict the meaning of the message. To be clear if the message is displayed to the user an error message would be appropriate to include an image that represents such an exclamation point on a red background or if the message is a warning associated with the classic image of an exclamation mark within in a yellow triangle.
To show how to perform this additional customization icon.png use the image (the one that identifies our application in the menu) located inside the folder res / drawable.
And 'Just insert this additional builder in the line of code:
miaAlert.setIcon (R.drawable.icon);
In the next chapter we're going to add buttons to AlertDialog and bind the listener.
| |
CSS (Course)
Web Design and Accessibility according to W3C CSS and XHTML. Starting from 29 €. |
| |
Java (Course)
OOP Programming in Java SUN. From 49 €. |
| |
MS Access (Advanced)
Learn how to create and manage databases quickly and easily. Starting from 29 €. |