I develop a game and want to show a dialog via toast : each sentence in different toast and showed one after other, but I can't find toast's event like on hide/on destroy.
Can I make something like this with toast ?
There's no event for Toast messages, they're built into the actual operating system and you cannot control or customise them apart from the duration, position, layout and contents.
If you fire multiple Toast messages one after the other they will be placed in a queue and the next one will be shown when the first one's duration finishes. In this way you could achieve what you want, however the system wasn't designed to do this. You should consider a View object in the view hierarchy that's on top of your other views to put your sentences in. This will allow you to have far more control over what you're showing the user as well as providing you with callbacks for when it is touched etc. This means that users can read the messages you're showing them at their own speed rather than only for the length of time you specify for a Toast duration.
Toasts are loaded into a queue when you call the Toast.show() function and are displayed one after another. Therefore, in your case, you should just be able to do something like...
Toast.makeText(context, "message 1" Toast.LENGTH_LONG).show();
Toast.makeText(context, "message 2" Toast.LENGTH_LONG).show();
Toast.makeText(context, "message 3" Toast.LENGTH_LONG).show();
and each subsequent Toast will display after the one previous to it is finished.
Related
Inside onCreate of Application class, I set its instance to a static field, then show all application Toasts through this context. All works good except one thing, in some places a Toast can be shown before first activity can even appear but Toast never appears or sometimes just flashes. I think its because Activity not shown or drawn yet ? Or I'm missing something.
Edit:
More like showing toast from onCreate of Application class
Edit 2 :
public class TestApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Test from App", Toast.LENGTH_LONG).show();
}
}
If you want to show a Toast without the application even started manually by the user you can register a BroadcastReceiver which listens to the BOOT_COMPLETED system broadcast and then start a Service which will handle your Toasts.
You'll find many examples on how to do this.
To make a Toast before drawing your layout resources, just do this following.
//put this code before your setContentView(R.layout.your_layout);
Toast.makeText(yourclassName.this,"your text here",5000).show();
Well, consider one thing. If you want this toast to be shown before loading your activity and notify users some message. Then it might not be possible always. Because, The time Toast is shown with fraction of nano/mili seconds your layout is being loaded as-well.Moreover, scenario is totally different when you are on a real device and on a emulator.This might be the cause you got a flash of your Toast message. Just run it on a real device and you will see the differences.
Hope that helps
I'm new to Robotium, I have two questions.
1) I'm trying to make click on custom listview item but its not working. I tried with clickInList(int) and clickInlist(int, int).
2) Handling random AlertDialog:
How to handle display alert dialog dynamically in Robotium? For example I'm using alert dialog when I get any message during call webservice, like connection failure, no internet, server error, timeout, etc..,
Thanks in advance.
There are two important things to note about the clickInList(int) method that aren't readily apparent: First, the list items are 1-indexed, so to click the first item of the list, use clickInList(1) not clickInList(0). Second, the clicking is relative to the visible items on the screen, so clickInList(1) will click the first visible item on the list, not the first item overall.
As for the dynamic handling of a Dialog, arbitrary pop-ups aren't really what Robotium was meant to handle. It's supposed to test user interaction with the app under known, controlled, repeatable conditions. If something unexpected happens in the middle of the test, such as losing connection, it should be considered a failure; There's a good chance your test wouldn't be able to run to completion anyway. As a hacky work-around, you can check for the existence of the Dialog before each of your events, something like:
if(solo.searchText("Dialog text") {
//handle closing dialog
}
However, I'd advise against this, it'll slow down your test considerably, and again, even if you close the dialog, the fact that the error happened in the first place is probably going to cause a later part of your test to fail.
I use Toast dialogs a lot throughout my application. However, I have noticed that after switching activities, the dialog will continue to stay visible until its timer has run out.
Toast.makeText( getApplicationContext(), R.string.toast_need_bt, Toast.LENGTH_LONG ).show();
I use Toast.LENGTH_LONG because the message is long and if the user decides to read it, the longer time option is needed. However once the user has used the application once or twice they will not need to read the toast messages and they will quickly move from activity to activity. However, these toast dialogs stay on the screen even while switching from activity to activity.
Is there a way to end all Toast Dialogs if the current Activity is terminated?
Call cancel() on the toast object when finishing/leaving the activity
Here is a link to the documentation Toast
Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.
Check this post for more answers on this topic How to cancel Toast
Even though there are a few answers already on how to use the .cancel() method, i would like to add a few options to this usecase:
1) Create in layout notifications Cyril Mottier's Article here
2) Display the toast only the first X times
3) Create a dialog with a "Show notifications" checkbox to allow the user to opt out.
I have seen a number of questions on cancelling toast. None of them is working.
I have a custom Toast. The code for that is all but one line same as http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
The difference is as follows.
The toast variable is defined as a class variable
The entire java code is written in a method.
In the start of this method, I have added the following line to cancel the toast.
if (toast!=null){
toast.cancel();
}
The method is called when user selects (onClick) the view/layout. The issue is when the user selects few times, the toast will get queued up (the toast.cancel is not working).
Any solutions?
[update]
I tried making toast object a static variable. Still dont work.
I suffered from same issue (custom toast queuing up) and found a solution. It worked fine in my case.
Having custom toast object initially set to null.
If this is null, create new custom toast object with "new".
As far as you are in same activity, don't "new" to create new object. Instead, use that object. Since setText() won't work in this case, use setView() as you do with your custom toast.
With this way show(), cancel(), show(), cancel() worked exactly as I expect. No delay, no queuing.
Hope this helps.
In the end, I created a Custom Dialog so that the user is blocked from doing anything else (and avoids multiple toasts popping up). Added a onClick Listener Event to close the dialog when user clicks the same.
Sad that Toast.cancel() doesn't work.
in above code toast.setDuration(Toast.LENGTH_LONG);
in that u use toast.setDuration(Toast.LENGTH_SHORT);
or se the particular time.Toast is cancelled automatically.we can't cancel it
Use this code for custom text:
LayoutInflater mInflater=LayoutInflater.from(context);
View view=mInflater.inflate(R.layout.tost_layout,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.show();
I was reviewing a student's program, which had code like this within an Activity:
Toast toast = Toast.makeText(this, "Hello", Toast.LENGTH_LONG);
toast.show();
toast.setText("Goodbye");
This displayed the text "Goodbye", which was initially quite a surprise. I assume this happened because the call to show() merely queues up a request to display the Toast instance and returns before it is actually displayed. The call to setText("Goodbye") mutates the instance before it is displayed.
Two questions:
Is my interpretation correct?
What's the best way to ensure serial semantics in the presence of Toast mutation?
When in doubt it's best to consult the source.
Toast internally uses a static reference to INotificationManager and calls it's enqueueToast method every time a Toast.show() is called.
It synchronizes around a List of Toasts so that only one Toast at a time is showed - this is needed if multiple Toast.show() are called then shows them one after another with their set duration.
Since Toasts references are enqueued (actually Toasts internal class TN is), calling setText() changes the enqueued Toast.