Android getapplicationcontext from anywhere

In this document

Key classes

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

Android getapplicationcontext from anywhere

If user response to a status message is required, consider instead using a Notification .

The Basics

ads

First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context. the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification with show(). as shown in the following example:

This example demonstrates everything you need for most toast notifications. You should rarely need anything else. You may, however, want to position the toast differently or even use your own layout instead of a simple text message. The following sections describe how you can do these things.

You can also chain your methods and avoid holding on to the Toast object, like this:

Positioning your Toast

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:

If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.

Creating a Custom Toast View

If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView(View) method.

Android getapplicationcontext from anywhere

For example, you can create the layout for the toast visible in the screenshot to the right with the following XML (saved as toast_layout.xml ):

Notice that the ID of the LinearLayout element is "toast_layout_root". You must use this ID to inflate the layout from the XML, as shown here:

First, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService() ), and then inflate the layout from XML using inflate(int, ViewGroup). The first parameter is the layout resource ID and the second is the root View. You can use this inflated layout to find more View objects in the layout, so now capture and define the content for the ImageView and TextView elements. Finally, create a new Toast with Toast(Context) and set some properties of the toast, such as the gravity and duration. Then call setView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show() .

Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View). If you do not have a custom layout to use, you must use makeText(Context, int, int) to create the Toast.

Related posts

  • Disney Movies Anywhere review: unifying your kids' content One complaint many a faithful Disney fan (or their DVD-buying parents) have had with buying Disney DVD combo packs is that the digital...

  • Files Anywhere App Free Download: Android Apps Files Anywhere Android app free apk created by FilesAnywhere download from Google Play and install Files Anywhere app on mobile phones and...

  • The Best Ereader for Android Reading ebooks on Android used to be just okay. Now it's awesome. While there are plenty of sweet apps for reading books on your phone or tablet, Play Books has...

  • Video: Hands-on With New Instagram Video on Android Not that it was much of a surprise after a couple of reports broke the news in the days leading up to today’s Facebook event, but yes, they...

  • 4 Pics 1 Word Answers & Cheats 4 Pics 1 Word answers, cheats, and hints will show you how to beat all levels and letters of the game for iPhone, iPod, iPad, and Android. Guess the word that the...