0X0nosugar 0X0nosugar - 11 days ago 4x
Android Question

How to position a toast in Nougat's multi-window mode?

While using Nougat's new multi-window mode, I noticed that a

will be displayed over another app if my own app is in the top window in portrait mode.

enter image description here

This is ... not good at all. So I tried positioning my
following this answer to "How to change position of Toast in Android?"

toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);

Unfortunately the
seems to be interpreted as "gravity for the screen" not "gravity for my window".

How to get a
displayed somewhere near the bottom of my window and centered horizontally?

enter image description here


I suspect this is the intended behaviour. Beyond there not being any straightforward way to achieve what you desire, according to the Material Design Guidelines Toasts are used primarily to convey system messages and should appear at the bottom of the screen.

My guess would be that any system messages should pertain to the device as a whole, and are not necessarily application specific, so this consistent behaviour makes sense. That said, I can see why this behaviour is undesirable, because the Toast ends up taking up practically 25% of the screen on one of the applications... however, regardless of the position of the Toast it will be gobbling up that screen real estate, whether it's on your application or the one below.

If you really wanted to implement a workaround, here is the implementation for offsetting the Toast vertically. A slight variation should achieve landscape mode, but it's a bit more finnicky.

View root = findViewById(R.id.root_main);

int[] xy = new int[2];

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
int devHeight = displayMetrics.heightPixels;

int viewHeight = root.getHeight();

Toast toast = Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG);
int yOffset = Math.abs(xy[1] - devHeight + viewHeight) + toast.getYOffset();
toast.setGravity(Gravity.BOTTOM, 0, yOffset);