I am tring to setText in another thread, that is, child thread. But for the following code, it is giving the error
Only the original thread that creat
Either you can use runOnUiThread
or use Handler
to set text in TextView.
Use runOnUiThread
for updating the UI control. In your case:
runningActivity.runOnUiThread(new Runnable() {
public void run() {
tv.setText(p + " %");
}
});
Edited:
Activity mActivity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mActivity= this;
...
..//The rest of the code
} //close oncreate()
thread{
mActivity.runOnUiThread(new Runnable() {
public void run() {
tv.setText(p + " %");
}
});
}
You need a reference to that textview and then do:
textView.post(new Runnable() {
public void run() {
textView.setText(yourText);
}
});
You can use handle
:
handler.post(new Runnable() {
public void run() {
textView.setText(yourText);
}
});
But your textView
and yourText
must be class fields.
In your thread (activity) where you create textView
use:
Handler handler = new Handler();
And pass handler
into another thread.