..
Come on in this chapter to deal with a widget last: ie the Toast. This object is used in many applications to show you a message of temporary duration comparable to a notification.
An example of use of this widget is to notify the user a message like "save made" or "performed" after the user takes some action. The feature of this widget is that it is by extremely simple because it has the possibility to show just a video message or an image without being able to interact with the user.
Now let's see the code to show video a toast with a text that we own. To do so we can resume an old project on the buttons and listeners, or (recommended to get familiar with the programming) to rewrite the code for a button to it, and the association of a OnClickListener. To be able to use Toast to import the class in our project:
android.widget.Toast
Posizioniamoci now within the OnClick method and insert these two lines of code:
Toast mioToast Toast.makeText = (HelloWorld.this, "This is a game created by Toast MrWebMaster" Toast.LENGTH_LONG); mioToast.show ();
We are going to carefully analyze the method and the prototype of makeText method is as follows:
makeText (Context context, CharSequence text, int duration)As a first parameter, the method requires the context in which to apply the toast. Since the code is inserted into the listener can not pass as this context because the listener passaremo as an argument to which the same is not applicable toast. It 'must be passed as a context for the main activity in order to show the video and then insert toast HelloWorld.this as a parameter.
In summary, the effect of the first piece of code is to instantiate an object of type Toast with the desired characteristics, yet, however, the toast is not visible. To do this you must invoke the show () method on the toast itself.
As we can see, in the first example shown, we have indicated our position our activity where Toast. To use the method to set the position of toast, you must first import the following class:
android.view.Gravity
To place the toast, for example, the center of the screen, just enter this line of code when you call the show () method:
setGravity (Gravity.CENTER, 0, 0);As can be seen as the first argument we pass the center of the screen and then the deviation is indicated on the x axis y axis from the point mentioned above.
And 'possible, as mentioned above, replace the text of the toast with a picture to show you.
In this case, of course, you can not use the method makeText, but we will use the constructor Toast (Context context), passing only the context (that of the Activity).
Then we use the method setview () to associate the image on our toast.
Here is the complete code to show unimmagine using Toast:
ImageView ImageView imageForToast = new (HelloWorld.this); imageForToast.setImageResource (R.drawable.icon); Toast Toast mioToast = new (HelloWorld.this); mioToast.setView (imageForToast); mioToast.show ();
| |
E-commerce with ASP (Ebook)
ECommerce and Shopping Cart with ASP. Only 35 €. |
| |
HTML (Course)
The markup language for the Web from 29 €. |
| |
Java (Course)
OOP Programming in Java SUN. -15% Discount until 12/10/2011. |