Search code examples
androidmultithreadingtoastandroid-runonuithread

How to show Toast messages from a thread in another class


I'm developing an app and I need to show a Toast message from a Thread that runs in another class. I Read about runOnUiThread but it doesn't works.. In main activty there is a call to another java class and here there is the connection to a webserver and I handle Http's messages from the server. Here, if I got 204 messages I need to show a toast. How can I implement runOnUiThread?

Thanks


Solution

  • Pass your activity's reference to that worker class and call the runOnUiThread like this

     activity.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(activity, "Your Message here", Toast.LENGTH_SHORT).show();
        }
    });