title: If any worker thread can update UI in android?
body: <p>I suppose that we can't update any UI <code>view</code> elements (<code>TextView</code>, <code>EditText</code> etc) in any worker thread but in the below example, I am able to update <code>views</code> from a worker thread, not all the times but only sometimes.</p>
  
  <p>See below example where I'm updating UI elements in worker thread - </p>
  
  <pre><code>public class AndroidBasicThreadActivity extends AppCompatActivity
  {
      public static TextView textView;
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_android_basic_thread);
  
          textView = (TextView) findViewById(R.id.textview);
  
          MyAndriodThread myTask = new MyAndriodThread();
          Thread t1 = new Thread(myTask, "Bajrang Hudda");
          t1.start();
      }
  }
  </code></pre>
  
  <p>Here is my worker thread -</p>
  
  <pre><code>class MyAndriodThread implements Runnable
  {
      @Override
      public void run()
      {
          AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
      }
  }
  </code></pre>
  
  <p>Believe me, I won't get any exception saying -</p>
  
  <pre><code>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  </code></pre>
  
  <p>And I can see the update UI on emulator.</p>
  
  <p>But if I do any heavy task (sleeping) in worker thread then I got the expected above exception, why it is happening like this? I am a new guy on android multithreading please get me out from this confusion.</p>
  
  <p>If i will change my worker thread to this i will get above exception -</p>
  
  <pre><code>class MyAndriodThread implements Runnable
  {
      @Override
      public void run()
      {
          try
          {
              Thread.sleep(2000);
          } catch (InterruptedException e)
          {
              e.printStackTrace();
          }
          AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread.");
          System.out.println("Child thread completed.");
      }
  }
  </code></pre>
  
  <p>And if my UI or main thread is waiting (join()) then i get exact output no exception at all, see this -</p>
  
  <pre><code>MyAndriodThread myTask = new MyAndriodThread();
          Thread t1 = new Thread(myTask, "Anhad");
          t1.start();
          try
          {
              t1.join();
          } catch (InterruptedException e)
          {
              e.printStackTrace();
          }
  </code></pre>
  
  <p><strong>Update</strong><br>
  First I thought it's rendering scheme then i changed my program by using ProgressBar..then i got really unexpected output... Still no exception mean i can update my progress bar in worker thread. See below-</p>
  
  <pre><code> @Override
      public void run()
      {
          for(int i = 1; i&lt;=10; i++)
          {
              try
              {
                  Thread.sleep(2000);
              } catch (InterruptedException e)
              {
                  e.printStackTrace();
              }
              activity.progressBar.setProgress(i);
          }
      }
  </code></pre>