Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom Toast at screen top

Please read the question before answering with your standard routine to print a Toast :)

I'd like to display a Custom Toast at the top left corner of the screen. I use this code to create the 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, in some devices, for example Samsung Galaxy S4, the toast is not positioned at (0,0), but with a margin like 40-50 pixels. Many other devices work as expected.

I am positive the margin is added by the WindowManager (The toast view is added as a View of type TYPE_TOAST to the WindowManager)

Why is that? Can it be modified? Please see the code below, I've cloned Toast.java into my own class and isolated the lines where the View is added to the 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 it's the WindowManager who is adding this margin on those devices. Looks like a safe area or something like that, but I cant find where (or if) this can be changed.

Help appreciated.

like image 810
rupps Avatar asked Nov 18 '14 06:11

rupps


People also ask

How do you make a toast appear at the top of the screen?

Positioning your Toast 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.

How do you show custom toast?

Approach. Go to res -> layout (right-click) -> new -> Layout Resource file -> Create (custom_toast_layout. xml) file. Add a CardView to contain the custom toast message and also add a TextView to display the text inside the custom toast message.

Can we set a custom layout for a toast?

If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method. Notice that the ID of the LinearLayout element is "toast_layout".

What is toast and custom toast in Android?

Toast is a subclass of Object class. In this we use two constants for setting the duration for the Toast. Toast notification in android always appears near the bottom of the screen. We can also create our custom toast by using custom layout(xml file).


2 Answers

I figured this out.

Starting 4.4+ you have to 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 positioned all the way to the top.

like image 172
Ali Avatar answered Sep 24 '22 00:09

Ali


Please try the following :

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

Also check the inflated layout, that might be causing the issue. You might be using the MatchParent for that view and gravity center.

like image 32
Eldhose M Babu Avatar answered Sep 24 '22 00:09

Eldhose M Babu