Accessing views from other thread (Android)

You can handle it in many ways,

  1. Try to use AsyncTask in this, your background work done in doInBackGround() method, and your UI will not block and you can also access the views of Activity from where you call AsyncTask by its context via publishProgress() and onProgressUpdate() .

  2. If you are using a simple Thread then using Handler or message or runOnUiThread you can update the view of main thread.

but, in your way I think AsyncTask is best for you.


You can solve the problem with android.os.Handler. Handler instance is bound to the thread that creates it, you can post Runnables to it and it will execute them in the thread it is bound to. For example:

import android.os.Handler;
import android.widget.TextView;

public class MyActivity extends Activity {
  private Handler uiHandler;
  private TextView simulationStatus;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    ...
    uiHandler = new Handler();
    ...
    simulationStatus = (TextView) findViewById(R.id.simulationStatus);
    ...
  }

  // This method is to be executed on the simulation thread.
  public void simulate() {
    ...
    final String simulationStatusText = ...;
    ...
    uiHandler.post(new Runnable() {
      @Override
      public void run() {
        // This is run on the UI thread.
        simulationStatus.setText(simulationStatusText);
        ...
      }
    });
  }

  ...
}

You can also use AsyncTask. See the link for example.


They can be accessed but only read-only. What you mean is you want to trigger changes from a thread on the views. From the worker thread (non-UI) these changes cannot be triggered, you need to use .runOnUiThread() or use Handlers. Use these at the point where you want to show something, e.g update the textview in .runOnUiThread(),

Example:

myThread = new Thread()
        {

            @Override
            public void run() {
                //yourOperation
                MyActivity.this.runOnUiThread(new Runnable(){

                    @Override
                    public void run() {
                        if(e!=null)
                        {
                            myTextView.setText("something");
                        }

                    }});
                super.run();
            }
        };
        myThread.start();