Android Update UI From Child Thread Example

Andriod UI is not thread safe. So you should update android UI in activity main thread, otherwise it will throw an exception. But there are also method to update android UI from child thread. This example will show you how to do that.

1. Update Android UI From Child Thread Steps.

  1. Create an instance of android.os.Handler in activity main thread.
  2. Override it’s handleMessage method, this method will be invoked when this handler get new messages from activity message queue.
  3. Update UI in handleMessage method.
  4. Create a android.os.Message object in child thread run method.
  5. Configure the message type by assigning message’s what attribute.
  6. Send the message object to activity main thread Handler object.

2. Update Android UI In Child Thread Examples.

If you update UI from child thread directly, then you will meet below exception.

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

android wong thread exception when update ui in child thread directly

update ui from child thread directly

So we should fix above problem use below code.

2.1 Main Activity Java File.

package com.dev2qa.example.thread;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dev2qa.example.R;

public class UpdateUiFromChildThreadActivity extends AppCompatActivity {

    private EditText changeTextEditor;

    private Button changeTextButton;

    private TextView changeTextTextView;

    // This is the activity main thread Handler.
    private Handler updateUIHandler = null;

    // Message type code.
    private final static int MESSAGE_UPDATE_TEXT_CHILD_THREAD =1;

    protected void onCreate(Bundle savedInstanceState) {

        setTitle(" - Update Ui In Child Thread Example.");

        // Initialize Handler.

        // User input text editor.
        changeTextEditor = (EditText)findViewById(;

        // Change text button.
        changeTextButton = (Button)findViewById(;

        // Show text textview.
        changeTextTextView = (TextView)findViewById(;

        // Click this button to start a child thread.
        changeTextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Thread workerThread = new Thread()
                    public void run() {
                        // Can not update ui component directly when child thread run.
                        // updateText();

                        // Build message object.
                        Message message = new Message();
                        // Set message type.
                        message.what = MESSAGE_UPDATE_TEXT_CHILD_THREAD;
                        // Send message to main thread Handler.

    /* Update ui text.*/
    private void updateText()
        String userInputText = changeTextEditor.getText().toString();

    /* Create Handler object in main thread. */
    private void createUpdateUiHandler()
        if(updateUIHandler == null)
            updateUIHandler = new Handler()
                public void handleMessage(Message msg) {
                    // Means the message is sent from child thread.
                    if(msg.what == MESSAGE_UPDATE_TEXT_CHILD_THREAD)
                        // Update ui in main thread.

2.2 Layout Xml File.



        android:hint="Input text and click below button to display in bottom text view."/>

        android:text="Change Text In Child Thread"/>

        android:text="This is original text."/>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.