Android: Using Toast in Worker Classes/Threads

  • May 17, 2010

Toasts can get messed up with the contexts if we are making and displaying it from a worker class and or thread such as inside a button listener.  We can use the Context Object to resolve this:

myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
String msg = "This is the Toast message";
int duration = Toast.LENGTH_LONG;
Toast.makeText(context, msg, duration).show();

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *