I would like to know how to create a TextView inside of a thread:
Thread t = new thread() {
public void run() {
for(int i = 0; i < 63; i++) {
TextView tv = new TextView();
tv.setText("What to do");
}
}
}
t.start();
What I don’t understand what to do is what is suppose to be inside the construct method for TextView?
To answer your question: to create a
TextViewyou must supply it with aContext.ActivityandApplicationboth extendContextand are most often used where aContextis necessary. In your case you should use the Activity whcih the createdTextViewwill be shown in. So, to modify your code:This is important, even though you didn’t ask about it: a
TextViewis a UI control. It is only legal to create a UI control on the UI thread. Creating it in a different thread may lead to all kinds of trouble. One way of doing this from a different thread is to useActivity.runOnUiThread()method:Disclaimer: Even though I fixed some mistakes in the code above, I didn’t test it. There can still be errors.