Programming Tips - How can I fix "method must be called from the UI thread" ?

Date: 2017aug31 OS: Android Language: Java Q. How can I fix "method must be called from the UI thread" ? A. You're now allowed to change the GUI from other threads. Here are some ways to deal with it. 1. Use Activity.runOnUiThread() You need to pass it a Runnable. eg:
activity.runOnUiThread(new Runnable() { @Override public void run() { mMyTextView.setText("Hello"); } });
This is easy since its standalone. And has been around since api level 1. 2. But if you are going it alot I think its better to send your UI thread a message and have a centralized handler in your UI thread. runOnUiThread() sends a message too but its your code its sending. Its nicer to send a message id (eg MY_SET_TEXT) which the UI thread processes. https://developer.android.com/training/multiple-threads/communicate-ui.html 3. Use AsyncTask. Its made for this. Your long running background task has a way of updating progress in the UI. Google for examples.