How to Use an Android Handler to Update a Textview in the UI Thread

How to use an Android Handler to update a TextView in the UI Thread?

There are several ways to update your UI and modify a View such as a TextView from outside of the UI Thread. A Handler is just one method.

Here is an example that allows a single Handler respond to various types of requests.

At the class level define a simple Handler:

private final static int DO_UPDATE_TEXT = 0;
private final static int DO_THAT = 1;
private final Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
final int what = msg.what;
switch(what) {
case DO_UPDATE_TEXT: doUpdate(); break;
case DO_THAT: doThat(); break;
}
}
};

Update the UI in one of your functions, which is now on the UI Thread:

private void doUpdate() {
myTextView.setText("I've been updated.");
}

From within your asynchronous task, send a message to the Handler. There are several ways to do it. This may be the simplest:

myHandler.sendEmptyMessage(DO_UPDATE_TEXT);

Using a Static Handler to update the UI thread

As you stated the docs, it says to use Looper.getMainLooper(), just change your code to:

MyHandler(Type reference) {
super(Looper.getMainLooper());
myWeakReference = new WeakReference<Type>(reference);
}

to update the UI from the main/UI thread.

how to update textview not from main thread?

Instead of using

textView.setText("text");

use

textView.post(new Runnable(){    
@Override
public void run(){
textView.setText("text");
}
};

This can be called from any Thread and will always execute the Runnable action on the UI Thread. Also, it does not require you to have a reference to a Context object, which saves you from making unneeded cross-references.

How can I update text of TextView in thread using kotlin

add UI thread surround settext or access any UI Widget inside UI thread

runOnUiThread {
textView.setText(str)
}

How to dynamically update textview inside recycler view with handler thread

You're not updating the textview on UI thread. Add this inside Runnable {}:

override fun run() {
holder.txthrs.setText(hour.toString() + ":" + min.toString() + ":" + sec.toString())
}

Update textView from thread

Any updates to the UI in an Android application must happen in the UI thread. If you spawn a thread to do work in the background you must marshal the results back to the UI thread before you touch a View. You can use the Handler class to perform the marshaling:

public class TestActivity extends Activity {
// Handler gets created on the UI-thread
private Handler mHandler = new Handler();

// This gets executed in a non-UI thread:
public void receiveMyMessage() {
final String str = receivedAllTheMessage();
mHandler.post(new Runnable() {
@Override
public void run() {
// This gets executed on the UI thread so it can safely modify Views
mTextView.setText(str);
}
});
}

The AsyncTask class simplifies a lot of the details for you and is also something you could look into. For example, I believe it provides you with a thread pool to help mitigate some of the cost associated with spawning a new thread each time you want to do background work.

Android update TextView in Thread and Runnable

The UserInterface can only be updated by the UI Thread. You need a Handler, to post to the UI Thread:

private void startTimerThread() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
private long startTime = System.currentTimeMillis();
public void run() {
while (gameState == GameState.Playing) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
}
});
}
}
};
new Thread(runnable).start();
}


Related Topics



Leave a reply



Submit