Custom toast on screen

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.

+6
source share
4 answers

I get it.

Starting with 4.4+ you should add the following flag:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) { mParams.flags = mParams.flags | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; } 

This will allow your toast to be located all the way to the top.

+3
source

Try the following:

 mFixedToast.setGravity(Gravity.TOP, 0, 0); 

Also check out the bloated layout that might cause the problem. You can use MatchParent for this view and gravity center.

+1
source
 private void Toast(String string) { // TODO Auto-generated method stub LinearLayout layout = new LinearLayout(this); layout.setBackgroundResource(R.color.Orange); TextView tv = new TextView(this); tv.setTextColor(Color.YELLOW); tv.setTextSize(25); tv.setGravity(Gravity.Top); tv.setText(string); ImageView img = new ImageView(this); img.setImageResource(R.drawable.ic_launcher2); layout.addView(img); layout.addView(tv); Toast toast = new Toast(this); // context is object of Context write tts.speak(string, TextToSpeech.QUEUE_FLUSH, null); // "this" if you are // an Activity toast.setView(layout); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } 
0
source

You can try this method to display a toast in the center of the screen. If you want to display a toast on top. You can change msg.getYOffset() . Here is the code. try it

 public static void displyToast(Context c, String str) { Toast msg = Toast.makeText(c, str, Toast.LENGTH_SHORT); msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2); msg.show(); } msg.getXOffset() / 2 take toast on center of screen horizintallly, whereas `msg.getYOffset() / 2` takes toast on center of screen vertically. 

Hope this helps you. Happy coding :)

0
source

Source: https://habr.com/ru/post/978315/


All Articles