java android handler timer in for loop -
i have 4 imagebutton , want change images of them randomly 2 seconds. doesn't work. problem here? don't understand work of handlers. want: change image of buttonimage, wait 5 seconds, change image of buttonimage
for (int = 0; < cpu_array.length; i++) { actual = cpu_rnd.nextint(4); cpu_array[i] = actual; switch (actual) { case 0: bluebutton.setimageresource(r.drawable.blue_a); break; case 1: redbutton.setimageresource(r.drawable.red_a); break; case 2: greenbutton.setimageresource(r.drawable.green_a); break; case 3: purplebutton.setimageresource(r.drawable.purple_a); break; default: break; } handler.postdelayed(new runnable() { @override public void run() { // after 2s = 2000ms bluebutton.setimageresource(r.drawable.blue); redbutton.setimageresource(r.drawable.red); greenbutton.setimageresource(r.drawable.green); purplebutton.setimageresource(r.drawable.purple); } }, 2000); }
this code worked me:
final button startbookmarksbutton = (button) findviewbyid(r.id.start_bookmarks_button); startbookmarksbutton.setbackgroundcolor(color.blue); new handler().postdelayed(new runnable() { @override public void run() { startbookmarksbutton.setbackgroundcolor(color.green); } }, 2000);
so think problem might somewhere else in code. sure random number logic in loop working ? why don't try getting delay working first , debug random part. or perhaps problem images.
Comments
Post a Comment