Search code examples
androidhandlerdelay

Android delay text


I want to set text to 25 buttons but have each text appear after 5 seconds. I've tried using tons of different methods. I tried using Thread.sleep(5000) but my program crashed. Then I attempted to create a Handler but it's crashing. Can someone please tell me why? Or is there another way I am supposed delay the text:

*Note: In my code I used the findViewById method but decided to not post it here for sake of simplicity and repetition -- so that is not the issue.

public class Game extends Activity {

protected List<String> my_list = new ArrayList<String>();
protected String letters[]; 
protected List<Button> button_list = new ArrayList<Button>();   
 Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, 
        b11, b12, b13, b14, b15, b16, b17, b18, b19, b20,
        b21, b22, b23, b24;

 Random rand = new Random();
 int random_counter;
 int my_list_counter = 25;
 int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);
    letters = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                           "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                           "u", "v", "w", "x", "y"};
    Button[] bttn_arr = new Button[] {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, 
            b11, b12, b13, b14, b15, b16, b17, b18, b19, b20,
            b21, b22, b23, b24};


    my_list.addAll(Arrays.asList(letters));

    button_list.addAll(Arrays.asList(bttn_arr));

    final Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            for (i=0; i<25; i++){
                random_counter = rand.nextInt(my_list_counter);
                button_list.get(i).setText(my_list.get(random_counter));
                my_list.remove(my_list.get(random_counter));
                my_list_counter--;
                handler.postDelayed(this, 5000);
            }
        }
    });
}   

}


Solution

  • Try adapting this instead:

    for (int i = 0; i < 20; i++) {
           final String message = "Hello" + i;
            Runnable x = new Runnable(){
                @Override
                public void run(){
                    Log.i("Hello", message);
    
                }
            };
    
           Handler handler = new Handler();
           //Run the code in runnable x at increasing time intervals of 5 seconds
           handler.postAtTime(x, SystemClock.uptimeMillis() + i*5000);
    
        }
    
    }
    


    This code will print out "hello" + i (ex. hello1 then hello2 then hello3 etc.) every 5 seconds. If you adapt your code to this it will run every 5 seconds. In the previous instance the handler was posting and running all the runnables at once, now it posts all the runnables at once, except with each one's start time increase by 5 seconds ( see handler.postAtTime(...) ) . Hope this helps!