I searched for a solution and couldn't find one so I'll ask here:
I'm trying to use setText command in the mainActivity, Until now I've used:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
textViewPrograss.setText(finalI + "");
}
});
now I'm trying to do the same thing, but from another class so i cant use:MainActivity.this.
I was trying to use code i found on another question with no success,This is the code:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d("UI thread", "I am the UI thread");
}});
Any suggestions?
You can use this snippet
textView.post(new Runnable() {
@Override
public void run() {
textView.setText("Text");
}
});
This (the second code sample from your question) is the correct way to access UI Thread from random location, although you should always try to have a Context to do this :-)
new Handler(Looper.getMainLooper()).post(
new Runnable() {
@Override
public void run() {
Log.d("UI thread", "I am the UI thread");
}});
It does work, and if it does not, check if you have debug logs enabled in your debugger ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With