When I use
Toast to display some popup text on the screen, it displays the text a little bit above the bottom of the screen, which is the default position.
Now I want to display it in the middle of screen or somewhere according to my choice.
Can anyone guide me how to achieve this?
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
x-positionoffset, and a
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.
As an aside if you get the error indicating that you must call makeText, the following code makes it work:
Toast toast= Toast.makeText(getApplicationContext(), "Your string here", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0); toast.show();