Toast toast = Toast.makeText(this, "Toast!!!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.AXIS_PULL_AFTER , 0, 0); toast.show();
what is the role of Gravity.AXIS_PULL_AFTER argument in order to positiong the Toast on the UI.
As per Android documentation:
Make a standard toast that just contains a text view.
Context: The context to use. Usually your Application or Activity
object.
Generally, this will be a reference to the activity calling Toast. However, if you are inside an anonymous class (for example, creating a click listener for a button), you will lose reference to your activity.
The parameter this is the object which tells the Toast where to show it, in your case is this your Activity which extends a Context. Note that this is used a lot around Android widgets. If you are using this inside an anonymous class e.g. an onClickListener(), use YourActivityName.this instead.
There is no clear specification for it according to Google documentation.
Its just a parameter that tells the current activity or application where makeToast function has to show Toast message. current activity or application
Related
https://developer.android.com/guide/topics/ui/notifiers/toasts: in the sources examples, they use getApplicationContext.
https://developer.android.com/reference/android/widget/Toast.html#makeText(android.content.Context,%20int,%20int): "Context: The context to use. Usually your Application or Activity object.".
What should we use in definitive?
To answer this question, I think one should think about...:
The lifecycle of the object Toast defined in a fragment, and then the lifecycle of Toast defined in an activity
The risk of memory leaks if we use getActivity, which depends on the Toast
https://blog.mindorks.com/understanding-context-in-android-application-330913e32514: here, it seems that one should use getActivity for Toast.
What should we use in definitive?
Generally speaking: if the Context is being applied to something for the UI, use the Activity, so that your current theme can be taken into account. Toast is UI; therefore, use the Activity.
In reality, I don't think that a Toast necessarily uses anything from the theme.
The lifecycle of the object Toast defined in a fragment, and then the lifecycle of Toast defined in an activity
Those are the same thing, since a fragment is unrelated to a Toast. You do not pass a Fragment to any method on Toast.
The risk of memory leaks if we use getActivity, which depends on the Toast
Since the Toast is short-lived, any possible leak is short-lived. Leaks are a problem when the leaked material is referenced for an indefinite period of time and so will not get cleaned up.
Is calling the method getActivity() once in my fragment and saving the reference in mActivity better than calling getActivity() every time I want to show a toast message?
Toast.makeText(mActivity, text, duration).show();
vs.
Toast.makeText(getActivity(), text, duration).show();
getActivity() should be preferred for 2 reasons:
1) Memory leak prevention
Having a variable mActivity lying around opens up opportunities for memory leak e.g. mistakenly set the variable as static, makes it easy and convenient to reference the activity in some running anonymous AysncTask
2) Correct nature of fragment-activity relationship
Fragments can be attached or detached at many point of times. Therefore, getting a reference of the activity hosting the current fragment should be on a on-demand basis. Having a mActivity variable means you need to set and unset it correctly.
Take note that what Toast requires here is a Context object so it's not necessarily the activity that is required here. An application context object would also suffice
Fragment wise both are same
First one
Activity mActivity = getActivity();
#Override
public void onClick(View arg0) {
Toast.makeText(**mActivity**,"Text!",Toast.LENGTH_SHORT).show();
}
Second one
use Directly like this
Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();
if you just need a Context or Activity, no difference. but if you want to access some method or field inside the parent activity, you would better saving the reference in mActivity.
If you want the context just to show the Toast messages and getting references to Activity is difficult for you then you can use getApplicationContext() instead.
I just learned that I can use either:
Toast.makeText(MainActivity.this, R.string.some_string,Toast.LENGTH_SHORT).show();
or,
Toast.makeText(getApplicationContext(), R.string.some_string,Toast.LENGTH_SHORT).show();
To display a Toast in Android.
Earlier I thought context was actually a sort of handle to the parent window where it should be display but the documentation is unclear about this.
Then I came across this table:
It also doesn't seem to mention what context exactly to use for a Toast?
Edit:
Is context like a "handle to parent window" for the sub-window like Toast? or does it actually allow the Toast.makeText to get access to resources or something?
Why is it being used at all if the context doesn't matter?
Looking at Toast.java, I can see the Context is used only for:
obtaining Resources
obtaining Package Name
getText which is actually same as #1
So apparently there's no difference whether it's Activity or ApplicationContext, unless those resources depend on theme (which is not the case as far as I can tell).
And no, the context passed to Toast is not a handle to parent window in any sense.
I'd recommend to use the activity in your case. Since you're calling from the activity itself. The activity is a Context and you're using the method on the activity to get another context (the application). It is a little unnecessary.
However in the case you're calling a toast from somewhere else it might be a better idea to use the application, since the application will always be there while your app is active.
You can show Toast only from UI (main thread) context. If you want show this from Service (but this is contradicts Google guidelines), you can do this way: Show toast at current Activity from service
For toasts, which are short-lived, you can usually use whatever context you want. Typically, you would use the activity context, but application context is fine as well.
I am trying to call a method available in a Activity from a java file.
The calling is made fine and the control transfers to the function by this code in the java file.
myActivity my = new myActivty();
myActivity.method1();
Now my problem is i cant able to give a toast message or display the alertdialog, Its giving null pointer exception when im using the Toast message,
07-20 15:13:00.836: ERROR/AndroidRuntime(418): java.lang.NullPointerException
Am i doing right?
Where am i wrong?
Help will be greatly appreciated.
Toast needs Context to show up. You're trying to show toast but you have created Activity without Context. Pass instance of working Activity to create dialog or show toast.
:) This wont work. You will need to pass some kind of a reference from your activity to the other Java class.
More in this here http://groups.google.com/group/android-developers/browse_thread/thread/741caff5a9536859?pli=1
I have a question. Is it possible to display a toast message (in if condition) from HttpDownload class to AnimalBadger class? (Both classes extend Activity)
if (((Node) textNodes.item(i)).getNodeValue().equals("a waning quarter moon"))
{
Toast.makeText(HttpDownload.this, "Some text...", Toast.LENGTH_LONG).show();
}
Thanks for the answers...
The first argument is just to get the Context to create the Toast with. You can use either activity or even getApplicationContext(). For simplicity, you usually use the closest available Context, which in this case would be your containing activity.
Toasts are not sent between application components, they take the form of small notifications usually at the bottom of the screen, and are a way to communicate low-priority messages to the user.
You may want to read the Creating Toast Notifications article in the documentation.
You could use a call back function and register it with HttpDownload class. That way the call back is called which will throw the toast(pun intended).