Quick question: Is there a way to display a toast message that doesn't fade away until I call cancel() on it?
I have tried setting the duration to something like 9999 but that doesn't work.
Is there a way to display a toast message that doesn't fade away until I call cancel() on it?
No, not directly from the SDK, but you can "tweak" your Toast to make it live longer by calling show() on it as many times you wish using threads. See this article for more information.
A Toast that doesn't go away until you cancel it is called a Dialog (or AlertDialog). The integer you pass to Toast.setDuration() is a flag - not a value - it will only recognize the values Toast.LENGTH_SHORT and Toast.LENGTH_LONG.
The Toast calss description says:
"A toast is a view containing a quick little message for the user. The
toast class helps you create and show those..."
"...The idea is to be as unobtrusive as possible, while still showing the
user the information you want them to see. Two examples are the volume
control, and the brief message saying that your settings have been
saved..."
As for the duration parameter it should be one of LENGTH_LONG or LENGTH_SHORT - 1 or 0 respectively.
Use a dialogue that looks like a Toast if you really have to, but I don't recommend doing this because this wont be what a user expects from a Toast.
Toast Message works with time.there is no way to control it with the cancel.You have to use Dialog for the kind of purpose
The official doc says (http://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context, int, int) ):
public static Toast makeText (Context context, int resId, int duration)
Since: API Level 1
Make a standard toast that just contains a text view with the text from a resource.
Parameters
context The context to use. Usually your Application or Activity object.
resId The resource id of the string resource to use. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
Throws Resources.NotFoundException if the resource can't be found.
This means there is no direct way to do that. You will have to build your custom code for this. As Toasts exacly overlap each other you can call the same Toast every second with a thread as an example, and use a cancel() custom method to terminate that thread.
I know this post is old but for others that come across it you are more than welcome to use a little library I put together called SuperToasts.
You can find the library here.
There is an indeterminate option for Toasts that are added to an Activity namely the SuperActivityToast. I purposely did not add this feature to standard SuperToasts, a class that mimics standard Toasts, because the SuperToast can linger until your application is killed as it is added to the WindowManager and not an Activity. SuperActivityToasts are added to the Activity's content and will be destroyed along with your Activity hence the ability to make them indeterminate.
Related
I'm working on my first Android application and I have a situation that really annoys me.
Lets say that user has to enter some information in few Edit Text fields and can't proceed if there is at least one empty field.
If user clicks a button to proceed and there is at least one empty field, a certain Toast is displayed.
The problem is, if user clicks that button like 10 times, and then enters those missing information and proceeds, those Toast messages keep showing in the new Activity along with Toast for successful registration.
Is there a way to interrupt a currently displayed Toast and display newest or something similar that will stop this kind of behaviour from happening?
edit: I have accepted #Vucko's answer because it suits better for my problem. #Augusto Carmo wrote the answer that indeed works, but I prefer Vucko's answer, it is just more elegant.
The more elegant and common way to do this would be to use:
editText.setError("Your error message");
This will disappear itself once you write something in your editText or you can manually remove it by calling setError(null);.
Have a single Toast object, and when you want to show another toast message, you cancel the last one and show the new one. Example:
Toast toast = new Toast();
toast.makeText(CONTEXT_HERE, MESSAGE_HERE, TIME_HERE).show();
// displaying another
toast.cancel();
toast.makeText(CONTEXT_HERE, ANOTHER_MESSAGE_HERE, TIME_HERE).show();
I know the basics of debugging, and I know I can add watches to a variable that stop the program's execution on a given condition. But I didn't want to stop the program every time I need to see the value of a variable. Neither I want to log the value of every relevant variable into logcat... I only wanted to see their values like I do at breakpoints, only in runtime.
I'm programming Android, in Android Studio.
Thanks for the help!
When your program has stopped on a breakpoint click the icon at the far right of the debugger menu (see image below). You can type in methods or variable names into this window and see what they would be.
You can type any expression you like (as long as it is within the scope of where you broke your code) and input any hard-coded values or objects all without re-running your project.
To add a variable to your watch list
Start by putting a break point in the class where you'd want to watch a specific variable. Run the code and once it hits your breakpoint from the Variables window frame you should see all of the variables that are accessible. Simply choose the one you'd want to watch and then right click and choose "Add to watches" from the drop-down.
Keep debugging and you should see the variable from the Watches window frame update when appropriate based on your code.
YES you can!
According to the Android Dev Summit '19, you can easily do that by disabling the Suspended flag in your breakpoint.
Then you can evaluate a log message to the console every time it gets the breakpoint, without suspending!
As you can see, my app fires a log to the console every time it gets to my breakpoint.
In other words, you can view variable changes at run time!
If you know the basics of debugging, you can easily add watches to a variable that stop the program's execution on a given condition. If you didn't want to stop the program every time you want to see the value of a variable then the easy way to see the value of a variable is to use Toasts.
A toast provides a sample value of any variable in an operation in a small popup. Toasts automatically disappear after a set timeout.
A simple code example:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
In order to see the variable value in a Toast:
int var=1;
Toast.makeText(getApplicationContext(), "vlaue is "+var, Toast.LENGTH_LONG).show();
In order to see the variable type in a Toast:
Toast.makeText(getApplicationContext(), "type is "+var.getClass().getName(), Toast.LENGTH_LONG).show();
I was reading over the android documentation for toasts, and noticed that the example code uses getApplicationContext() rather than getActivity() or this. From the docs:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Based on other sources, I have been given to understand that using getApplicationContext() is generally bad practice. Are toasts somehow an exception? If so, why? Or are the Android docs just wrong in this case?
I have been given to understand that using getApplicationContext() is generally bad practice
I would describe it more as "use Application when you know why you are using Application". Too many Android developers have negligible Java experience, get confused by inner classes, and think that they need to call getApplicationContext() (or getBaseContext()) to get a Context to pass as a parameter to something or another.
Dave Smith's epic blog post on the role of different Context implementations covers a fair number of the common use cases... though Toast is not among them.
Are toasts somehow an exception? If so, why?
Toasts work with Application as the Context, though there is no particular need to use an Application to show a Toast.
Or are the Android docs just wrong in this case?
They are not wrong, insofar as the code works. The JavaDocs for Toast in various places point out that Activity is also a fine Context to use, and I see nothing in the Toast source code to indicate otherwise.
It's important to note that Toast can be used even when your context is not visible or is not in control of any UI. In other words, the documentation is pointing out that you can have a minimal context (like that from a service) and still use Toast.
I don't believe that the documentation is trying to present a "best practice" for the use of Context, but rather to properly demonstrate this attribute of Toast.
I had a problem with Toast localization string when used application context. It worked properly with activity.
I have a back-end process running (AsyncTask). I am interested in printing out System.out.println(); to the screen (something like the console in Eclipse) with information so the user is informed which calculations are running in the back.
What I wish to accomplish is seeing the text running on the screen - something like a dialog on top of the currenly-in-focus activity.
Does anyone got experience with this?
Thanks, D.
I don't think you can hijack System.out.print() and send it to the screen. But you can make a TextView and send the new lines of text to it as they are ready. Something like this should work.
String outputStr = "";
//Whenever you want to add a line to the TextView do it like this:
outputStr += "\n" + [yourNewText];
mTxt.setText(outputStr);
If you were to add lots of lines in this manner would appear to the user as the same sort of situation as the eclipse console.
Since you can do this with a TextView you could then add that TextView to a custom dialog and show it to the user.
I believe you also need to be in the UI thread to update a textview. In an asynctask, you call onProgressUpdate, which is then executed in the UI thread. Otherwise, you can use view.post or you can use a Handler.
I'm new to Android(and Java also) and I'm trying to understand what are Fields in predefined Android classes.
For example in the Android View class there are Fileds: EMPTY_STATE_SET, ENABLED_FOCUSED_SELECTED_STATE_SET etc.
Please can you tell me how to understand this and how can I use it? For me it looks like some constants but it's not.
Typically they are constants for the class, to be used in situations like this:
myWidget.setColor(Widget.BLACK_AND_TAN);
They usually have integer values, but they make the code more readable this way, and also allow for later changes to the API without altering the behavior of previous code. (Widgets.BLACK_AND_TAN == 7, or Widgets.BLACK_AND_TAN ==15,could both be true, or any other number. It doesn't matter. Just that BLACK_AND_TAN always corresponds to Black and Tan coloring.)
edit: note that Widget is a made up class, as is the BLACK_AND_TAN constant. let me get a real example for you.
Real Example, used to re-position the cursor every time a text field is updated:
DefaultCaret caret = (DefaultCaret)outputArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
Note that these do not HAVE to be constants as we normally refer to them; for example
Toast.LENGTH_LONG;
can be user defined, but it does specify how long a toast should display