ui-thread
What is the Android UiThread (UI thread)
The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread. For instance, let’s say your application is a … Read more
How do we use runOnUiThread in Android?
Below is corrected Snippet of runThread Function. private void runThread() { new Thread() { public void run() { while (i++ < 1000) { try { runOnUiThread(new Runnable() { @Override public void run() { btn.setText(“#” + i); } }); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); }
Can’t create handler inside thread that has not called Looper.prepare()
You need to call Toast.makeText(…) from the UI thread: activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(activity, “Hello”, Toast.LENGTH_SHORT).show(); } }); This is copy-pasted from another (duplicate) SO answer.