How to change position of Toast in Android?

ID : 10007

viewed : 33

Tags : androidtoastandroid

Top 5 Answer for How to change position of Toast in Android?

vote vote


From the documentation,

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:

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.

vote vote


If you get an error indicating that you must call makeText, the following code will fix it:

Toast toast= Toast.makeText(getApplicationContext(),  "Your string here", Toast.LENGTH_SHORT);   toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);; 
vote vote


You can customize the location of your Toast by using:

setGravity(int gravity, int xOffset, int yOffset)


This allows you to be very specific about where you want the location of your Toast to be.

One of the most useful things about the xOffset and yOffset parameters is that you can use them to place the Toast relative to a certain View.

For example, if you want to make a custom Toast that appears on top of a Button, you could create a function like this:

// v is the Button view that you want the Toast to appear above  // and messageId is the id of your string resource for the message  private void displayToastAboveButton(View v, int messageId) {     int xOffset = 0;     int yOffset = 0;     Rect gvr = new Rect();      View parent = (View) v.getParent();      int parentHeight = parent.getHeight();      if (v.getGlobalVisibleRect(gvr))      {                View root = v.getRootView();          int halfWidth = root.getRight() / 2;         int halfHeight = root.getBottom() / 2;          int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;          int parentCenterY = ((gvr.bottom - / 2) +;          if (parentCenterY <= halfHeight)          {             yOffset = -(halfHeight - parentCenterY) - parentHeight;         }         else          {             yOffset = (parentCenterY - halfHeight) - parentHeight;         }          if (parentCenterX < halfWidth)          {                      xOffset = -(halfWidth - parentCenterX);              }             if (parentCenterX >= halfWidth)          {             xOffset = parentCenterX - halfWidth;         }       }      Toast toast = Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT);     toast.setGravity(Gravity.CENTER, xOffset, yOffset);;        } 
vote vote


 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);  toast.setGravity(Gravity.CENTER, 0, 0);; 
vote vote


From the documentation:

Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method (setGravity) is a no-op when called on text toasts.

Which means that setGravity can no longer be used in API 30+ and will have to find another to achieve the required behaviour.

Top 3 video Explaining How to change position of Toast in Android?