Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display a Toast from a background thread on Android?

How can I display Toast messages from a thread?

like image 811
Arutha Avatar asked Jun 28 '10 17:06

Arutha


People also ask

Can I show toast from background thread?

You can't show a Toast on a thread that is not the activity's ui thread.

How do you show toast on Android messages?

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

You can do it by calling an Activity's runOnUiThread method from your thread:

activity.runOnUiThread(new Runnable() {     public void run() {         Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();     } }); 
like image 182
Lauri Lehtinen Avatar answered Sep 29 '22 08:09

Lauri Lehtinen