I am trying to use a toast from another class.
In class 1 I have the toast method:
public static void textToast(String textToDisplay)
{
Context context = getApplicationContext();
CharSequence text = textToDisplay;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER, 50, 50);
toast.show();
}
I am trying to call this toast from another class but when I make the method static, it says cannot make a static reference to this method getApplicationContext().
I access it by using class2.textToast("");
Any advice on this would be appreciated. Thanks
If you want to provide a method which should be valid for different contexts (e.g. activities), pass this context as parameter.
If you want to call this method from nested inner classes (as is often the case), you can use
thisas context(or implement
textToastin the outer class and call it viaOuterClass.this.textToastfrom the nested inner class)