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
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
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();
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);
}
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.