Please read the question before responding with the standard standard procedure for printing toast :)
I want to show a custom toast in the upper left corner of the screen. I use this code to create Toast:
Toast mFixedToast = new Toast(getApplicationContext()); mFixedToast.setDuration(timeout); mFixedToast.setView(myInflatedLayout); mFixedToast.setGravity(Gravity.TOP|Gravity.FILL_HORIZONTAL, 0, 0); mFixedToast.setMargins(0,0);
However, on some devices , such as the Samsung Galaxy S4, the toast is not located at (0,0), but with a margin of 40-50 pixels. Many other devices are working properly.
I am sure that the field has been added to WindowManager (the toast view has been added as a view of type TYPE_TOAST for WindowManager)
Why? Can I change it? Please see the code below, I cloned Toast.java into my class and highlighted the lines in which the View is added to WM:
// LayoutParams for the TOAST view ... tried to change params.type but no luck. final WindowManager.LayoutParams params = mParams; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; params.windowAnimations = android.R.style.Animation_Toast; params.type = WindowManager.LayoutParams.TYPE_TOAST; mWM = (WindowManager)mView.getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); mParams.gravity = gravity; // CHECKED these all are 0 !!! mParams.x = mX; mParams.y = mY; mParams.verticalMargin = mVerticalMargin; mParams.horizontalMargin = mHorizontalMargin; . . if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this+" with "+mX+","+mY+","+mVerticalMargin+","+mHorizontalMargin); mWM.addView(mView, mParams);
So, it looks like WindowManager is adding this margin to these devices. It looks like a safe area or something like that, but I can't find where (or if) it can be changed.
Help evaluate.
source share