Learn How to Position the Toast Message

Today in this tutorial, we will discuss about how to position the toast message in Android applications.

Although I assume you must know what is Toast in android, but for beginners, I will define it in brief. A toast is the simple popup message which provides simple feedback about an operation. By default it is displayed at the bottom of the Application, but sometimes we don’t want it to be at the bottom. You should read my previous posts on toast messages if not yet read the list below:

The code which we are going to develop will show how to create toast messages in different positions by altering Gravity. You will learn how to alter the standard position of toast with the help of setGravity(int, int, int) method. This method accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example the following code will show the Toast at top-left corner with X-Offset and Y-Offset as 50 and 100 respectively.

 

 

Follow the steps below to create the code example discussed above:
Step 1: Create a new android application in your IDE.  

Step 2: Write following into your activity_main.xml:

 

 

 

Step 3: Write following into your MainActivity.java:

 

 

 

Step 4: Run for the output as shown in the video below:

 

That’s it!!!

Leave a Comment: