How to Set Text of Text View in Another Thread

How to set text of text view in another thread

You need a reference to that textview and then do:

textView.post(new Runnable() {
public void run() {
textView.setText(yourText);
}
});

in Kotlin:

val textView: TextView = findViewById(R.id.textView)
textView.post(Runnable { textView.setText(yourText) })

Update text in a TextView from a Thread

Use the following which is the recommended way to update Widget on UiThread

game.getTextView().post(new Runnable() {
@Override
public void run() {
game.getTextView().setText(Integer.toString(score));
}
});

Hope this helps.

TextView.setText not working due to threading?

For background work use handler(use this code)

 new Handler().post(new Runnable() {
@Override
public void run() {
// here do the background task

}
});

and if u want to update UI use runOnUiThread()

              if u want  to update ui Just do this
runOnUiThread(new Runnable() {
@Override
public void run() {
// here do the UI chnages
}
});

Hope this will help you.

Do like this

  @Override
public void run() {
// Always cancel discovery because it will slow down a connection
//Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
while (true) {
//counter++;

try {
output = "";
//read the data from socket stream
//mmInStream != null && counter%10000000 == 1
if(mmInStream != null) {
mmInStream.read(buffer);
for(byte b : buffer)
{
char c = (char) b;
if(c >=' ' && c <'z') {
// System.out.print(c);
output += c;
}

}
// context == activity context
context.runOnUiThread(new Runnable() {
@Override
public void run() {
// here do the UI chnages
// you can set text in textview Here
}
});

}
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
//an exception here marks connection loss
//send message to UI Activity
break;
}
}

}

});

Setting text on TextView from background thread anomaly

From the very first try, your code is wrong because you created Handler on different thread. This leads to looper/handler is run on different thread. Based on your comment, I guess you know this issue and you want to understand why exception doesn't throw on first try but second.

You should beware this: Accessing UI Element on different thread except from UI Thread causes undefined behavior. This means:

  • Sometime you see it works.
  • sometime you don't. You will meet exception as you have seen.

That means Accessing UI Element on different thread isn't always 100% reproducible.

Why you should access all UI Elements only on UI Thread? Because processing UI Element (change internal state, draw to screen ...) is a complex process and need to synchronized between related parties. For example, you call TextView#setText(String) on 2 fragments that both visible on screen. Android doesn't do this concurrently but pushing all jobs into an UI message queue and do that sequentially. This is also true not only from your application viewpoint but also from whole Android system perspective. Updating from status bar that called by system, updating from your app that called by your application always push actions to same UI Message queue before processing.

When you access and modify UI elements on different thread, you broke that process. That means maybe two threads might access and modify an element at same state and same time. As the result, you will meet race condition at some time. That when error occurs.

Explaining for your situation is hard because not enough data for analyzing. But there are some few reasons:

  • At your first try, TextView haven't displayed on screen yet. So different thread was able to make change on TextView. But on your second try, you sleep for 1 second. At this time, all view has rendered and displayed successfully on screen, so exception threw. You can try Thread.sleep(0) and hopefully your code doesn't crash.
  • This is will happen in some situations but hard to guess why. That by some chance, both your thread and ui thread accesses same lock object, exception threw.

You can read more about thread issue here Android Thread

Explicit references

Many tasks on non-main threads have the end goal
of updating UI objects. However, if one of these threads accesses an
object in the view hierarchy, application instability can result: If a
worker thread changes the properties of that object at the same time
that any other thread is referencing the object, the results are
undefined.

Hope this help you.

How to set textview from another thread

You need to update the GTK# text view from the GUI thread. You can do this by using Gtk.Application.Invoke:

 Gtk.Application.Invoke (delegate {
textview1.Buffer.Text = "Whatever";
});

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