How to display Toast in a thread : Android

If you are working in a non UI thread and you try to display a Toast message, you will get an error message like,

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.(
    at android.widget.Toast.(
    at android.widget.Toast.makeText(

⛏️ To solve this you need to use runOnUiThread() method

myActivity.runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(myActivity, "Toast Message Text!", Toast.LENGTH_LONG).show();

Recent Posts:

Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.