Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a toast from a non activity class?

Tags:

android

toast

I have a class that I am using to get GPS data within my activity. In the constructor I pass it the activity's context:

gpsFetcher = new GPSFetcher(this);

and in the gpsFetcher class I have:

this.context = c.getApplicationContext();

OR just

this.context = c;

and then I call the toast with:

Toast.makeText(context, "sometext", Toast.LENGTH_LONG);

But it never shows up... Is there something I'm missing? Is it possible?

Thanks!

like image 343
Matt Avatar asked Apr 04 '11 20:04

Matt


People also ask

How do you show toast in a non activity class?

new YourOtherClass(this). showToast(message); // showToast doesn't have to take a Context as argument, it could just take one as constructor parameter and hold that. // But then you have to make sure YourOtherClass is not used anymore if the Activity is closed.

Which is the correct way to create and display a toast?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.


1 Answers

Are you forgetting Toast#show?

Toast toast = Toast.makeText(context, "sometext", Toast.LENGTH_LONG);
toast.show();
like image 55
Matthew Willis Avatar answered Oct 24 '22 21:10

Matthew Willis