8

I have several classes in my application. Some are Activities, Services and Pure java classes. I know i can display a Toast message from within an Activity but i'd like to display a Toast from a pure java class.

In the java class i pass a context in to the constructor but this doesn't seem to show the toast.

I have created a method in the Application class that takes a String as an argument, hoping i could generate a Toast using the Application context, no joy here either.

How can i generate a Toast from a class that isn't 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();

    }
turtleboy
  • 8,210
  • 27
  • 100
  • 199
  • @kalyanpvs Hi, yes i have tried it and it does display the message but the Toast message does not disappear. It stays on the screen even if i exit the app. How can i remove the message after a few seconds. I've read somewhere it's to do with creating the toast outside a context?? – turtleboy Dec 23 '13 at 11:57
  • @turtleboy toast message disapper after their some time..you dont remove the toast message after some time.. – kalyan pvs Dec 23 '13 at 12:06
  • @kalyanpvs yes it should disappear after a time which is determined by Toast.LENGTH_LONG. It is not however, it remains on the screen indefinitely. Any ideas why? – turtleboy Dec 23 '13 at 12:09
  • @turtleboy once debub and check that method is how many times calling.. – kalyan pvs Dec 23 '13 at 12:11

7 Answers7

6

First create Application class like this..

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 class like ApplicationContext.showToast("your string");

be careful about context object leaking..

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • 1
    got this error: ```java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference``` – kangear Sep 29 '15 at 12:19
  • @kangear your string is null..how you are passing string?? – kalyan pvs Sep 30 '15 at 05:26
6

There are two ways you can do that, if you have a valid context, you can do it like @CapDroid wrote:

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

if you don't, you can send a Context as well,

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 shoh toast.

hope that helps.

Shahar
  • 3,692
  • 4
  • 25
  • 47
4

Write this method in your Application Class. You just need to pass message in parameter from any Activity.

public void showToast(String message)
{
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1

It worked for me with :

Toast.makeText(this.getContext(), R.string.title, Toast.LENGTH_LONG).show();
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Guest123
  • 11
  • 7
1

Need to pass the context to the 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();

    }
0
Toast.makeText(getActivity(), "Index....."+index, Toast.LENGTH_LONG).show();
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
ashish
  • 217
  • 1
  • 11
0

Pass the message from other class using function parameter

public void showToast(String message) {

Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

}