Reputation: 363
I'm having the exact opposite problem of this post - specifically, I want to display a toast in the default location (centered, just above the status bar at the bottom) but it is always appearing horizontally and vertically centered.
Here is the code and call I am using to display the toast (toastNavigation
method is in a separate class from call):
public static void toastNavigation(Context context, CharSequence message,
int duration, int gravity, int gravity_xOffset, int gravity_yOffset) {
Toast toast = Toast.makeText(context, message, duration);
toast.setGravity(gravity, gravity_xOffset, gravity_yOffset);
toast.show();
}
toastNavigation(this,
"My message", Toast.LENGTH_SHORT, Gravity.NO_GRAVITY, 0, 0);
Why would my toast be centered even though I am passing in the constant that indicates "...no gravity has been set."? Is there some other constant I should pass to clear GRAVITY constants inherited from the context?
Upvotes: 2
Views: 5318
Reputation: 4679
For me the default location of a toast without a gravity setting is between 1/3 and 1/4 up from the bottom.
I've found Gravity.BOTTOM, which is often cited as the default, actually sets the gravity to the very bottom of the screen. Also I've found Gravity.NO_GRAVITY sets the toast in the center of the screen, just like Gravity.CENTER, instead of the actual default location. This is rather irritating. I've yet to find a setting that sets a toast back to it's true default location, I've just had to make a new toast.
Upvotes: 1
Reputation: 543
I checked your code to display toast on screen. Do one thing, please remove call to setGravity method, then your toast will appear on exact location, as per your requirement. If you try to set NO_GRAVITY as gravity of your toast, then in this case toast will pick default gravity, and value of default gravity for toast is Centered in window(same as for dialog). Please let me know for any concern. Thanks.
Upvotes: 0
Reputation: 63293
I'm going to assume you have good reason for calling setGravity()
at all if you just need the default values.
The default gravity settings for a Toast are Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM
, so you can apply those to get the default placement.
You can see this in the source for Toast...check the initialization of the mGravity field.
Upvotes: 4
Reputation: 24857
If you would like to display a Toast in teh default location just call it like so:
Toast.makeText(context, message, duration).show();
This will display the toast in the standard location. My guess is the NO_GRAVITY is not the default setting for a Toast. It probably uses gravity bottom and applies a y value to bring it up from the bottom of the screen.
Upvotes: -2