302

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?

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
UMAR-MOBITSOLUTIONS
  • 73,009
  • 94
  • 197
  • 273

12 Answers12

435

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.

Pentium10
  • 190,605
  • 114
  • 394
  • 474
  • 11
    what are the integer values there? Are they dpi? or what is the max? – clifgray Jun 26 '13 at 15:29
  • 11
    Might be pointing out the obvious, but `Gravity.CENTER_VERTICAL` will put the toast in the middle of the screen. – Felix Aug 30 '13 at 01:15
  • 3
    x and y offset are in pixel, so the maximum is your display width / height. – paulgavrikov Oct 01 '13 at 22:47
  • 2
    @Pentium10 The docs state that the offsets are in pixels. Should I assume that these are "px" units, as opposed to "dp" units? – batbrat Feb 24 '14 at 04:15
  • My toast messages on my new galaxy s6 show in 2 different positions. first in the left horizontal center vertical and then moves down to center horizontal, bottom vertical. this effect does not happen on any of my older test devices. I display each message twice in row since any screen taps kill the first toast right away., – Androidcoder Mar 19 '16 at 20:43
  • I've found Gravity.NO_GRAVITY does not actually set the toast to the default position of 1/4 to 1/3 up from the bottom, it sets to the center just like Gravity.CENTER. In fact, once a new gravity is explicitly set I don't see how one could get it back to the actual default. Also, toasts don't stack for me, they just replace. – Androidcoder Nov 03 '19 at 21:10
161

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);
toast.show();
HB.
  • 3,514
  • 3
  • 18
  • 41
Rymnel
  • 4,265
  • 3
  • 24
  • 28
16

You can customize the location of your Toast by using:

setGravity(int gravity, int xOffset, int yOffset)

docs

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 - gvr.top) / 2) + gvr.top;

        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);
    toast.show();       
}
JDJ
  • 4,198
  • 3
  • 22
  • 43
12
 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
 toast.setGravity(Gravity.CENTER, 0, 0);
 toast.show();
nzala
  • 524
  • 6
  • 10
8
Toast mytoast= Toast.makeText(getApplicationContext(), "Toast Message", 1);  
mytoast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);  // for center horizontal
//mytoast.setGravity(Gravity.CENTER_VERTICAL);       // for center vertical 
//mytoast.setGravity(Gravity.TOP);                       // for top
mytoast.show();

The above code is will help u to display toast in the middle of screen or according to ur choice for that just set the toast gravity according to ur need

Note: For this process u have to use object of Toast

Anik Islam Abhi
  • 24,324
  • 8
  • 52
  • 74
user3652986
  • 129
  • 2
  • 5
5

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.

lasec0203
  • 1,784
  • 16
  • 32
abdulwasey20
  • 539
  • 6
  • 18
3

The method to change the color, position and background color of toast is:

Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
View view=toast.getView();
TextView  view1=(TextView)view.findViewById(android.R.id.message);
view1.setTextColor(Color.YELLOW);
view.setBackgroundResource(R.color.colorPrimary);
toast.show();

For line by line explanation: https://www.youtube.com/watch?v=5bzhGd1HZOc

Gorgon_Union
  • 535
  • 2
  • 8
  • 22
Mithun Adhikari
  • 409
  • 4
  • 13
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – greg-449 Jul 25 '17 at 17:14
2

setting toast at topin screen

toast.setView(view);
toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
 toast.setDuration(Toast.LENGTH_LONG);
 toast.show(); 

now at bottom

 toast.setView(view);
 toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
 toast.setDuration(Toast.LENGTH_LONG);
 toast.show();  

the same way we can set toast in left, right and also center

Click here

Abhishek
  • 51
  • 2
1

//A custom toast class where you can show custom or default toast as desired)

public class ToastMessage {
            private Context context;
            private static ToastMessage instance;

            /**
             * @param context
             */
            private ToastMessage(Context context) {
                this.context = context;
            }

            /**
             * @param context
             * @return
             */
            public synchronized static ToastMessage getInstance(Context context) {
                if (instance == null) {
                    instance = new ToastMessage(context);
                }
                return instance;
            }

            /**
             * @param message
             */
            public void showLongMessage(String message) {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }

            /**
             * @param message
             */
            public void showSmallMessage(String message) {
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
            }

            /**
             * The Toast displayed via this method will display it for short period of time
             *
             * @param message
             */
            public void showLongCustomToast(String message) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
                TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
                msgTv.setText(message);
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();


            }

            /**
             * The toast displayed by this class will display it for long period of time
             *
             * @param message
             */
            public void showSmallCustomToast(String message) {

                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
                TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
                msgTv.setText(message);
                Toast toast = new Toast(context);
                toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setView(layout);
                toast.show();
            }

        }
Amardeep
  • 1,246
  • 8
  • 17
1
Toast toast = Toast.makeText(this, "Custom toast creation", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
    toast.show();
  • 4
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Joe Ferndz Oct 12 '20 at 18:09
1

For people like me, looking for an answer to set the gravity on Toasts on Android R and above, see this article.

Jorn Rigter
  • 364
  • 1
  • 15
1
 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);

 toast.setGravity(Gravity.CENTER, 0, 0);

 toast.show();

//this is the best solution to counter any error

Syscall
  • 16,959
  • 9
  • 22
  • 41
Shailendra
  • 37
  • 6