-1

I have a toast message and the default setting is displaying it bottom-center on the screen. I am wondering how to position it TOP-center. Any ideas?

Thank you

  • Official doc : [Android Toast](https://developer.android.com/guide/topics/ui/notifiers/toasts.html) look for `setGravity()` – Bharatesh Apr 18 '15 at 12:53
  • possible duplicate of [How to change position of Toast in Android?](http://stackoverflow.com/questions/2506876/how-to-change-position-of-toast-in-android) – Melquiades Apr 18 '15 at 12:57
  • Try to use setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0); – Haresh Chhelana Apr 18 '15 at 12:58

3 Answers3

3

From the docs:

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.

So in your case, you could do:

//create toast object
Toast myToast = Toast.makeText(getApplicationContext(), greetings[rndy.nextInt(6)], Toast.LENGTH_SHORT);
//set gravity
myToast.setGravity(Gravity.CENTER_HORIZONTAL); //<-- set gravity here
//and show it
myToast.show();
Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • Yes I've read the doc, but I'm unsure of the x and y values to use because I'm thinking they will vary depending on the device that is used?? BTW cool Zoso symbol :) Led Zep!! – zombetelgeuse Apr 18 '15 at 12:53
  • 1
    @ub3rfunk: Use values of 0 and 0, but [use `Gravity.CENTER_HORIZONTAL`](https://developer.android.com/reference/android/view/Gravity.html#CENTER_HORIZONTAL) instead of `Gravity.LEFT` for horizontal centering. – CommonsWare Apr 18 '15 at 12:55
  • 1
    Like @CommonsWare said, use 0,0. If you read the last paragraph from the docs aboev, it says that x,y are used to nudge the position = slightly change. Use Gravity flags for general positioning. – Melquiades Apr 18 '15 at 13:05
  • OK. Where do I place the setGravity line of code? Here is my toast message right now: Toast.makeText(getApplicationContext(), greetings[rndy.nextInt(6)], Toast.LENGTH_SHORT).show(); – zombetelgeuse Apr 19 '15 at 12:21
  • Many thanks for the help @Melquiades, it's working quite nicely now...cheers – zombetelgeuse Apr 20 '15 at 20:55
2

I implemented this a while ago for one of my projects. This places the toast right below whatever view you want it to be. This approach is usually used to override a button's long click to give a short description of what the button does

Below is the button where we want to

private View.OnLongClickListener mShareFriendsOnLongClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        int offsetY = 10;//getResources().getDimensionPixelSize(R.dimen.toast_offset_y);

        Toast toast = Toast.makeText(mContext, R.string.share_with_friends, Toast.LENGTH_SHORT);
        ScrapbookUtils.positionToast(toast, v, getWindow(), 0, offsetY);
        toast.show();
        return true;
    }
};

And then the actual method that does the work. This utility can make you place your toast wherever you want on your screen.

public static void positionToast(Toast toast, View view, Window window, int offsetX, int offsetY) {
    // toasts are positioned relatively to decor view, views relatively to their parents, we have to gather additional data to have a common coordinate system
    Rect rect = new Rect();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    // covert anchor view absolute position to a position which is relative to decor view
    int[] viewLocation = new int[2];
    view.getLocationInWindow(viewLocation);
    int viewLeft = viewLocation[0] - rect.left;
    int viewTop = viewLocation[1] - rect.top;

    // measure toast to center it relatively to the anchor view
    DisplayMetrics metrics = new DisplayMetrics();
    window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.widthPixels, MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.heightPixels, MeasureSpec.UNSPECIFIED);
    toast.getView().measure(widthMeasureSpec, heightMeasureSpec);
    int toastWidth = toast.getView().getMeasuredWidth();

    // compute toast offsets
    int toastX = viewLeft + (view.getWidth() - toastWidth) / 2 + offsetX;
    int toastY = viewTop + view.getHeight() + offsetY;

    toast.setGravity(Gravity.LEFT | Gravity.TOP, toastX, toastY);
}
ichthyocentaurs
  • 2,173
  • 21
  • 35
1

If you don't want to use the simplest way of Toast toast = Toast.makeText(context, text, duration).show(), you can customize your Toast. Here is my codes, you can try this:

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

If you are still not content, there is a project in Github called SuperToast. If you study it, I think you will be inspired a lot.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78