How to setText from a separate thread in Android

I want setText from inside Thread.

This is my theme code:

private class GenerateThread implements Runnable { public void run(){ // generate the first music music = generate(prevmusic, prevmusic.length); prevmusic = music; // write the midi writeMidi(music, song); textOut.setText("Initialising..."); }); } } 

in my main code, I use

 Thread t = new Thread(new GenerateThread()); t.start(); 

This does not allow me to setText from the stream. After some posts on the Internet, I tried using a handler, but it gave me errors, I think I define Runnable twice this way.

Handler handler (to main)

 private class GenerateThread implements Runnable { public void run(){ handler.post(new Runnable() { // generate the first music music = generate(prevmusic, prevmusic.length); prevmusic = music; // write the midi writeMidi(music, song); textOut.setText("Initialising..."); }); } } 

How can I setText from Thread? Thanks!

+4
source share
2 answers

besides runOnUiThread there is also View#post(Runnable) , which I would prefer here because you do not need ugly references to external activity ( MyActivity.this.runOnUiThread() ).

 private class GenerateRunnable implements Runnable { public void run() { // this code is executed in a background thread. // generate the first music music = generate(prevmusic, prevmusic.length); prevmusic = music; // write the midi writeMidi(music, song); textOut.post(new Runnable() { public void run() { // this code is executed on the UI thread. textOut.setText("Initialising..."); } }); } } @Override protected void onResume() { super.onResume(); new Thread(new GenerateRunnable()).start(); } 

Also do not confuse Runnable and Thread . A Runnable is just a regular class with the run() method. This can be and is often done on the new Thread . If you want, you can also make GenerateThread real Thread as follows:

 private class GenerateThread extends Thread { @Override public void run() { // code here. } } // start somewhere new GenerateThread().start(); 

In addition, using the classic Thread , you can also think about using AsyncTask , since this is done specifically for tasks that do something for a long time, and should update the user interface after that or when there is progress.

+4
source

You can update the user interface only from the user interface stream. runOnUiThread will allow you to run an action in the user interface thread the next time it runs. You can do something like:

 runOnUiThread(new Runnable() { public void run() { textOut.setText("Initialising..."); } }); 

EDIT:

 private class GenerateThread implements Runnable { public void run(){ // generate the first music music = generate(prevmusic, prevmusic.length); prevmusic = music; // write the midi writeMidi(music, song); // Update the UI MyActivity.this.runOnUiThread(new Runnable() { public void run() { textOut.setText("Initialising..."); } }); } } 
+3
source

Source: https://habr.com/ru/post/1434854/


All Articles