Going to take the comments I made and post them here as well:
"I want the main thread blocked" -- No, you want the UI disabled during the period, not blocked, which is why they're all occurring at once. A better option would be a global boolean, let's call it "enabled," and set that to false during the operation, then to true when the updates have completed, while calling postInvalidate() from the secondary thread. Then in your UI thread you should check to see if enabled is equal to true before allowing any additional operations to take place. This will create the illusion of being blocked without it actually being blocked
Now, taking your code:
boolean moveon;
//...
onCreate() {
moveon = false;
//...
Handler myh = new Handler();
Runnable myr = new Runnable() {
public void run() {
Resources res = getResources();
for(int i=0; i<3; i++){
id = res.getIdentifier("txt"+Integer.toString(winners[i]), "id",
getApplicationContext().getPackageName());
TextView temp = (TextView)findViewById(id);
temp.setBackgroundColor(Color.parseColor("#ffd700"));
temp.getBackground().setAlpha(127);
temp.invalidate();
}
moveon = true;
}
};
myh.post(myr);
while(moveon == false){}
You're close:
boolean moveon;
//...
onCreate() {
moveon = false;
//...
// Still in UI thread here
Handler myh = new Handler();
final Resources res = getResources(); // final so we can access it inside second thread
new Thread(new Runnable() {
public void run() {
// In second thread
for(int i=0; i<3; i++){
// Now we use the Handler to post back to the UI thread from the second thread
myh.post(new Runnable() {
public void run() {
// In UI thread, update here and then invalidate
id = res.getIdentifier("txt"+Integer.toString(winners[i]), "id",
getApplicationContext().getPackageName());
TextView temp = (TextView)findViewById(id);
temp.setBackgroundColor(Color.parseColor("#ffd700"));
temp.getBackground().setAlpha(127);
temp.postInvalidate();
}
});
Thread.sleep(delayMs); // Sleep second thread
}
myh.post(new Runnable() {
public void run() {
// In UI thread, reset the moveon to true on correct thread
moveon = true;
}
});
}
}).start();
Then don't use your while-loop, as that will still just block the UI and give you the same problem. What you want to do instead is check if moveon == true before allowing the user to do anything else; no loop necessary.