Android Update UI From Child Thread Example

Andriod UI is not thread-safe. So you should update the android UI in the android activity main thread, otherwise, it will throw an exception. But there are also methods to update android UI from the 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 gets new messages from the activity message queue.
  3. Update UI in the handleMessage method.
  4. Create an android.os.Message object in child thread run method.
  5. Configure the message type by assigning the message’s what attribute.
  6. Send the message object to the activity main thread Handler object.

2. Update Android UI In Child Thread Examples.

If you can not watch the above video, you can see it on the youtube URL

  1. If you update UI from the child thread directly, then you will meet the below exception.
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  2. So we should fix the above problem using the 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.

  1. activity_update_ui_from_child_thread.xml
            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.