SoFunction
Updated on 2025-03-01

Android programming implementation example update UI in child thread using handler

This article describes the Android programming implementation using handler to update the UI in child threads. Share it for your reference, as follows:

MainActivity Code:

package ;
import ;
import ;
import .;
import ;
import ;
public class MainActivity extends AppCompatActivity {
  private TextView textView;
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      (msg);
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    textView = (TextView) findViewById();
    new Thread(){
      @Override
      public void run() {
        ();
        try {
          (2000);
          (new Runnable() {
            @Override
            public void run() {
             ("ok");
            }
          });
        } catch (InterruptedException e) {
          ();
        }
      }
    }.start();
  }
}

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="">
  <TextView
    android:textSize="40sp"
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />
</RelativeLayout>

For more information about Android related content, please check out the topic of this site:Summary of the usage of Android threads and message mechanisms》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.