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

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -