Position a Toast in Android Application any where you like.

A toast provides simple feedback about an operation as a simple popup message. It is usually displayed at the bottom of the screen, centered horizontally. You can alter this position with the help of setGravity(int, int, int) method. This method accepts three parameters: a Gravity constant(Gravity.TOP,Gravity.BOTTOM,Gravity.LEFT,Gravity.RIGHT), an x-position offset, and a y-position offset.
Below piece of code will display the Toast at top-left corner with X-Offset and Y-Offset as 100 and 200 respectively.

 Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);  
 // Set the Gravity to Top and Left  
 toast.setGravity(Gravity.TOP | Gravity.LEFT, 100, 200);  
 toast.show();  

To show the Toast in Top Right corner with X-Offset and Y-Offset as 100 and 200 respectively:

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);  
 // Set the Gravity to Top and Right 
 toast.setGravity(Gravity.TOP | Gravity.RIGHT, 100, 200);  
 toast.show();  

To show the Toast in Bottom Left corner with X-Offset and Y-Offset as 100 and 200 respectively:

Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);  
 // Set the Gravity to Bottom and Left
 toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 100, 200);  
 toast.show();  

To show the Toast in Bottom Right corner with X-Offset and Y-Offset as 100 and 200 respectively:
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);  
 // Set the Gravity to Bottom and Right 
 toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 100, 200);  
 toast.show(); 

Comments