Display Toast message from Application class

I have several classes in my application. Some of these are classes, services, and pure Java classes. I know that I can display a Toast message from within an Activity, but I would like to show Toast from a pure java class.

In the java class, I pass the context to the constructor, but that doesn't look like a toast.

I created a method in the Application class that takes a string as an argument, hoping that I can generate Toast using the application context, without joy.

How can I create a Toast from a class that is not a service or Activity, etc.

public class LoginValidate{ public LoginValidate(Context context) { this.context = context; nfcscannerapplication = (NfcScannerApplication) context .getApplicationContext(); } public void someMethod(){ nfcscannerapplication.showToastMessage(result); } } 

.

///, then in my application class

 public void showToastMessage(String message){ Toast.makeText(this.getApplictionContext(), "Encountered a problem with sending tag: " + message, Toast.LENGTH_LONG).show(); } 
+7
android android-context toast
source share
6 answers

First create an application class as follows.

 public class ApplicationContext extends Application { /** Instance of the current application. */ private static ApplicationContext instance; /** * Constructor. */ public ApplicationContext() { instance = this; } /** * Gets the application context. * * @return the application context */ public static Context getContext() { if (instance == null) { instance = new ApplicationContext(); } return instance; } /** * display toast message * * @param data */ public static void showToast(String data) { Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show(); } } 

call this method from any of your classes, for example ApplicationContext.showToast("your string");

be careful with the leak of the context object.

+6
source share

There are two ways to do this, if you have a valid context, you can do this as @CapDroid wrote:

 public static void showToastWithTitle(String title) { Toast.makeText(getApplicationContext(), title, Toast.LENGTH_LONG).show(); } 

if you don’t, you can also send context,

 public static void showToastWithTitleAndContext(Context context, String title) { Toast.makeText(context, title, Toast.LENGTH_LONG).show(); } 

Note that you can define a static Context in your Application.java and use it to call the toh toast.

hope this helps.

+4
source share

Write this method to your application class. You just need to pass the message in the parameter from any operation.

 public void showToast(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } 
+2
source share

Need to pass showToastMessage(String message) context showToastMessage(String message)

Like this showToastMessage(String message, Context context)

// then in my application class

 public void showToastMessage(String message, Context context){ Toast.makeText(context, "Encountered a problem with sending tag: " + message, Toast.LENGTH_LONG).show(); } 
+2
source share
 Toast.makeText(getActivity(), "Index....."+index, Toast.LENGTH_LONG).show(); 
0
source share

This worked for me:

 Toast.makeText(this.getContext(), R.string.title, Toast.LENGTH_LONG).show(); 
0
source share

All Articles