Android Update Textview in Thread and Runnable

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();
}

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.

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 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.

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.

Not updating TextView value with thread,Why?

Try this, I think it will solved your problem.

public class MainActivity extends AppCompatActivity {

private Random random;
private Handler handler;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv_number);
displayRandomNumber();
}

/**
* Display random number in text view
*/
private void displayRandomNumber()
{
random = new Random();
handler = new Handler();

handler.postDelayed(new Runnable() {
@Override
public void run() {
int value = random.nextInt();
textView.setText(String.valueOf(value));
handler.postDelayed(this,2000);
}
}, 2000);
}
}


Related Topics



Leave a reply



Submit