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 Update UI From Child Thread Example

Android Update UI From Child Thread Example

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;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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_ui_from_child_thread); setTitle(" - Update Ui In Child Thread Example."); // Initialize Handler. createUpdateUiHandler(); // 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() { @Override public void onclick(View view) { Thread workerThread = new Thread() { @Override 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. updateUIHandler.sendMessage(message); } }; workerThread.start(); } }); } /* Update ui text.*/ private void updateText() { String userInputText = changeTextEditor.getText().toString(); changeTextTextView.setText(userInputText); } /* Create Handler object in main thread. */ private void createUpdateUiHandler() { if(updateUIHandler == null) { updateUIHandler = new Handler() { @Override 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. updateText(); } } }; } }}

2.2 Layout Xml File.


<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/change_text_editor" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Input text and click below button to display in bottom text view."/> <Button android:id="@+id/change_text_in_child_thread_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Change Text In Child Thread"/> <TextView android:id="@+id/change_text_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is original text."/></LinearLayout>
  • 15