---
title: "Android for loop not working in main thread"  
description: "Android for loop not working in main thread"  
author: "Anonymous User"  
published: 2014-11-12  
updated: 2014-11-13  
canonical: https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread  
category: "android"  
tags: ["java", "for loop"]  
reading_time: 2 minutes  

---

# Android for loop not working in main thread

I try to [rotate](https://answers.mindstick.com/qa/34548/what-is-rotate-indefinitely) [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character). i used [runnable](https://www.mindstick.com/forum/160962/what-is-the-difference-between-callable-and-runnable-in-java) to rotate image,i wrote [for loop](https://www.mindstick.com/forum/1825/how-to-implement-nested-for-loop-into-linq) and i want to rotate image inside for loop this is a my [source](https://www.mindstick.com/interview/840/what-happens-if-the-both-source-and-destination-are-named-same)\

```
public void rotateimages(final View myView) {    myHandler = new Handler();    runnable = new Runnable() {        @Override        public void run() {            double k = 30;            double speed = Math.PI / k;            for (alpa = 0; alpa < (Math.PI * 6 + res * 2 * Math.PI / 5); alpa += speed) {                myView.setRotation((float) (myView.getRotation() - alpa));                Log.e("alpa values", String.valueOf(alpa));                k = Math.min(k + 0.2, 240);                speed = Math.PI / k;                myHandler.postDelayed(this, 100);             }            // rotateimages();        }    };    myHandler.postDelayed(runnable, 180);}
```

when i run my app i can rotate only one time.what is a [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) in my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)? if [anyone](https://www.mindstick.com/articles/23207/how-to-find-the-best-fit-job-for-anyone) knows solution please help me\

## Replies

### Reply by Tom Cruser

If you want to loop , you need to define this 'runnable' loop again itself edit your code to:

```
myHandler = new Handler();count = 0;public void rotateImage(final View myView, final int size) {    runnable = new Runnable()
{        @Override        public void run() {            count++;            myView.setRotation(myView.getRotation()+ size);            if(count ==10){              
myHandler.removeCallBack(runnable );            }           myHandler.postDelayed(this,1000); // 1000 means 1 second duration         }        };        myHandler.postDelayed(runnable,180); // 180 is the delay after new runnable is going to called}How to loop itself, that's mean one code line, or one statement call the event again. Some example: void nothing(int a){   if(a> 0)     nothing(a-1);   }and call nothing(10). that's all, and avoid some action
like:void nothing(int a){       for(int i =0; i<a;i++)          nothing(a-1);   //BIG WRONG       }
```

Do you know your solution?


---

Original Source: https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
