Android Toast moves around on screen
Asked Answered
S

3

15

My android app displays several Toast messages. I recently installed it on a Galaxy S6, running Android 5.1.1 and noticed that the messages are displayed initially around the center of the screen, then they move to proper position (near bottom, if no Gravity is specified), then back to the initial position before fading away.

Context context = getApplicationContext();
String newMsg = getString(R.string.wild_card_msg);
Toast mToast = Toast.makeText(context, newMsg, Toast.LENGTH_LONG); 
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.show(); 

Update:

  • I have upgraded support libraries as well as set compile-sdk and target sdk to the latest API. That did not fix the issue
  • I have removed all .setGravity() calls. No change.
  • I have noticed that Toast messages behave properly at the first execution after installation (be it in USB debug mode or via download from PlayStore), but the issue reoccurs at (all) subsequent runs.
  • I have also discovered that my Toast messages disappear immediately if I touch the screen (anywhere). I thought Toast displays cannot be influenced by user interaction.

Anyone else having this issue, know how to fix it or know a workaround?

Please note that I have accepted Nick's answer, proposing snackBar as a workaround.

Stagecoach answered 5/2, 2016 at 17:36 Comment(15)
You should provide the code you are using to show your toast. This indeed would be very strange.Derzon
Thank you, David, for your quick response. Here's an example: Context context = getApplicationContext(); String newMsg = getString(R.string.wild_card_msg); Toast mToast = Toast.makeText(context, newMsg, Toast.LENGTH_LONG); mToast.setGravity(Gravity.CENTER, 0, 0); mToast.show();Stagecoach
PLease edit your question and dont past code in the comments. also, is there a reason why you set a gravity on your toast? That's the first time I've seen someone do thatDerzon
What happens if you remove the call to setGravity()?Fay
(Didn't see the "edit" up there ... thanks for doing that for me). In the specific example above, I set a specific gravity to avoid hiding a relevant part of the screen that I want to remain visible to the user. However, that has probably nothing to do with the strange behavior, as that happen also to simpler Toast statements with no gravity.Stagecoach
Application context? Toast and dialogs have a very strange behaviour (even crashes) when invoked with application context. Try to change to activity context.Barabarabarabas
@Barabarabarabas - Thx for your suggestion. I had already tried "this", Application and Activity context with no success. Note that the same code works perfectly on an older device running android 4.4.2.Stagecoach
I'm having the exact same issue. It's not just on toasts. Anything that uses animation also has the same issue. Works fine on the first run after a fresh install, then breaks after that. So far it only seems to happen on the S6 or S6 edge. As a clue: If I turn off animations the problem goes away for my quick menu animations. This is not possible however for Toasts because their animations can not be turned off. Also, even default Toasts have the same issue. Gravity and custom views do not effect this.Stickney
remove this line: mToast.setGravity(Gravity.CENTER, 0, 0);Thekla
Potentially sounds like a framework issue where the window being created for the toast is getting resized which is resulting in the window size getting changed resulting in the recalculation of the views based on gravity based on the window size.. Just a guess. Seems like one way to avoid this is to shift over to dialogs for HTC, unless you want to re-write a custom Toast that has a custom animation.. if that's even possible.'Duncan
If it happens wilth all animations I'd imagine its a measurements issue, could you post the layout it occurs on (and any code that youre programatically adding/creating views)Mcmorris
@theJosh. Thanks a lot for "sponsoring" my question - I'm receiving interesting answers now - and for sharing your experience on this issue.Stagecoach
@JoxTraex and Nick. Thank you for your comments. I have also suspected an issue of framework, dimensions, layout, screen fitting or similar. However, I thought Toast messages would have no dependencies to the layout and just "float" on top of whatever is currently displayed. My (game) program is lengthy and so is my mainActivity xml layout, I don't think it's appropriate to post it in full. For info, I did move some Toast's to Dialog's, when applicable.Stagecoach
Following @JoxTraex comment: you can check if a weird resize is going on with hierarchy viewer developer.android.com/tools/help/hierarchy-viewer.htmlCoastland
@Coastland - Thx for this generally useful tip. Unfortunately it didn't help in this particular case, as the tool could only return several errors (Unable to get view server version from device; Unable to get view server protocol version from device; Unable to debug device; Missing forwarded port), whilst the device name appears properly in these error messages as well as in the main device window.Stagecoach
M
7

Your question asked for a fix or workaround. The simplest workaround is (in my opinion) also the best option, because it moves you to using the more modern components: Switch to a snackbar.

Simple Snackbar:

//on a fragment you can simply use getView(), otherwise give it the root view of your
//layout so that the snackbar can use it to find context

Snackbar.make(getView(), "The toast text", Snackbar.LENGTH_SHORT).show();

It's in the support design library for compatibility.

This is a snackbar:

enter image description here

And some of the support / design libraries that can be included in gradle are

compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
Mcmorris answered 25/2, 2016 at 9:37 Comment(6)
Thx, I definitely want to try this option out. I'll setup my program accordingly (I suppose I need to add a compile statement in the Gradle build script and more), test this out and share the outcome.Stagecoach
Sure, I use the ones I just added to my answerMcmorris
I managed to get the snackBar solution to work. Rounded shape Toast's are somewhat more "sexy" ... but snackBar's include essentially equivalent functionality (and offer more potential). Above all, they don't wonder around on my S6 screen! In summary: it is a great workaround to my problem and I formally "accepted" your answer. Thank you Nick!Stagecoach
@DiorDNA This does not answer the question, toast and snackbar is not the same thing, for instance you can't show a snackbar from a background service.Opia
You shouldnt be showing a toast from a background service either. The question was looking for a solution to the jumping toast or a workaround to avoid this issue. This is the latter so of course it answers the question.Mcmorris
@Pedram. I have not given up on the lead you provided (your answer below). However, it is unlikely that the (French) operator will do anything about it. Your answer might be academically correct, but I have accepted Nick's because it provided a pragmatically viable workaround.Stagecoach
O
3

It is because of the customized version of Android that vendors deploy with their products, It is true for Activity animations too, You can achieve the desired behavior on all devices by extending the Toast class and providing your own animation and style or using an open source library for showing toast messages, for activities setting a custom animation for activities creates a constant animation on all devices.

Opia answered 24/2, 2016 at 21:11 Comment(1)
Thx, your answer sounds plausible. One of the 2 S6's on which I have the problem comes from a (European) operator. I'll check with them (and other customers). For the remaining of your answer, I'm aware there are custom options, but I'd like to get standard Toast (or equivalent) to work first.Stagecoach
T
-2

try this simple Toast

Toast.makeText(getApplicationContext(),"Your message", Toast.LENGTH_SHORT).show();
Tufted answered 24/2, 2016 at 5:46 Comment(3)
@Rajen he did not said that he tried like this, he said he tried with "this", "activity" etc. let him comment whether he tried this or not.Tufted
check this "(Didn't see the "edit" up there ... thanks for doing that for me). In the specific example above, I set a specific gravity to avoid hiding a relevant part of the screen that I want to remain visible to the user. However, that has probably nothing to do with the strange behavior, as that happen also to simpler Toast statements with no gravity. – Dior DNA Feb 5 at 18:15'Bushnell
@Tufted (and Rajen). Thx. Indeed, I have also tried the suggested form, unsuccessfully.Stagecoach

© 2022 - 2024 — McMap. All rights reserved.