Android: Error trying to edit button text after thread sleep

Posted by Vass on Stack Overflow See other posts from Stack Overflow or by Vass
Published on 2012-10-15T15:35:04Z Indexed on 2012/10/15 15:37 UTC
Read the original article Hit count: 284

(programming Android in Eclipse) I am trying to set up a delay in changing the text in a button. I am getting errors only after there is a delay and the text needs to be changed. Here is the simplified code without a while loop:

final Button button_target = (Button) findViewById(R.id.button_target);        
    Thread textChange = new Thread(new Runnable() { 
        public void run(){
            button_target.setText("fog");
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){}                
        }
    });
    textChange.start();

And now here is the code where a change of text on the button is required after the sleep which now causes and error and exit (forced):

 final Button button_target = (Button) findViewById(R.id.button_target);        
    Thread textChange = new Thread(new Runnable() { 
        public void run(){
            button_target.setText("cat");
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){}                
            button_target.setText("dog");
        }
    });
    textChange.start();

what am I doing to cause the error? Is there another method that I should do to be able to invoke a sleep or delay to the thread so that a text change operation can be performed?

(the actual code has a while loop but I believe this form puts the error in highlight)

© Stack Overflow or respective owner

Related posts about android

Related posts about android-textview