title: switching an image widget in timeline
body: <p>I want to create an android widget with switching image with time delay. I have no idea about how to do this. Should I create Thread in methood onUpdate?
  this example of my layout.xml file:</p>
  
  <pre><code>    &lt;ImageButton
          android:id="@+id/imageViewWGT"
          android:background="@drawable/dog" /&gt;
  </code></pre>
  
  <p>and this is what I want to known how to do correct:</p>
  
  <pre><code>public class MainActivity extends AppWidgetProvider {
      private int counter = 0;
      private ImageView wgt_img;
      wgt_img = wgt_img.findViewById(R.id.imageViewWGT);
  
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
          new Thread(new Runnable() {
              @Override
              public void run() {
                  while (true) {
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
  
                              if (counter%2 == 0){
                                  wgt_img.setImageResource(R.drawable.dog);
                              }else
                                  wgt_img.setImageResource(R.drawable.cat);
  
                          }
                      });
                      counter++;
                      try {
                          Thread.sleep(1000);     // sleep 1 sec
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }).start();
      }
  }
  </code></pre>