Hurry! 20% Off Ends SoonRegister Now

Toast Notification in Android

User Interface Components

You don’t have to build all of your UI using View and ViewGroup objects. Android provides several app components that offer a standard UI layout for which you simply need to define the content.

Notifications

A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

The name itself implies their functionality. Notification on Android can be done in any of the following ways:

If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform:  “Android training”  Course.  This course will help you to achieve excellence in this domain.

They are

  1. Toast Notification
  2. Status Bar Notification
  3. Dialog Boxes

Checkout:-Android tutorial

Toast Notifications

A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user’s current activity remains visible and interactive. The notification automatically fades in and out and does not accept interaction events. 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:

Context context = getApplicationContext();

CharSequence text = "Hello Sai_tech!";

int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

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.

MindMajix Youtube Channel

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

Toast.makeText(context, text, duration).show();<br><br>

Positioning your Toast

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

Checkout Android Interview Questions

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

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

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.

Explore Android Sample Resumes! Download & Edit, Get Noticed by Top Employers!  Download Now!

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Android TrainingDec 24 to Jan 08View Details
Android TrainingDec 28 to Jan 12View Details
Android TrainingDec 31 to Jan 15View Details
Android TrainingJan 04 to Jan 19View Details
Last updated: 08 Oct 2024
About Author

I am Ruchitha, working as a content writer for MindMajix technologies. My writings focus on the latest technical software, tutorials, and innovations. I am also into research about AI and Neuromarketing. I am a media post-graduate from BCU – Birmingham, UK. Before, my writings focused on business articles on digital marketing and social media. You can connect with me on LinkedIn.

read less