Android Why does this line of code execute after the for loop? -


  btnswitch2.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 btnswitch2.setimageresource(r.drawable.on);                 myvib.vibrate(50);                 strobeflash();             }         });      }  private void strobeflash(){                     for(int i=0; i<10;++i){                 turnonflash2();                 turnoffflash2();             }     } 

the code above executes when button pressed, change picture of image button. however, when button pressed, loop executes first line above

btnswitch2.setimageresource(r.drawable.on); 

executes. there i'm doing that's causing loop execute first?

actually loop execution faster changing image resource. can add postdelay() between ui update , loop execution. solve issue.

          btnswitch2.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     btnswitch2.setimageresource(r.drawable.on);                     myvib.vibrate(50);                     handler=new handler();                     runnable r=new runnable() {                             public void run() {                                 strobeflash();                                      }                     };                     handler.postdelayed(r, 3000);                  }             });          }         private void strobeflash(){                         for(int i=0; i<10;++i){                     turnonflash2();                     turnoffflash2();                 }         } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -