The short answer is you need to read up on something called a
"handler". It will probably take a few pages to explain the ideas in
detail.

But here are some notes I have kept

http://www.satyakomatineni.com/akc/display?url=DisplayNoteIMPURL&reportId=3496&ownerUserId=satya

Here is the short answer:

1. when you respond to the menu item, so called the main thread is
executing that code. Any repainting will take place after you return
from thread. Until then you are setting and resetting some variables.
So the final one will take affect.

2. So you need to let go the main thread before the background can take affect

3. However you can use something called a "handler" to post a message
to yourself in half a sec to respond to

You can download a sample project here called TestHandlers.zip and
tinker with it

http://www.satyakomatineni.com/akc/display?url=ShowAttachmentsIMPURL&reportId=3468&downerUserId=satya

Anyways there are probably a number of samples on the net around "handlers".

Good luck
Satya
http://www.androidbook.com
http://www.satyakomatineni.com



On Fri, Jul 2, 2010 at 4:13 PM, Dmitri Snytkine <[email protected]> wrote:
> Hello!
> I am new to android development, also new to java. I have many years
> of experience with OO PHP, so it's not hard for me to quickly learn
> Java and Android, but I still don't know many things like Thread
> class.
>
> I just started developing a simple app.
>
> What I want to do is that when a button is clicked, then the TextView
> is update in this way:
> the background is changed to black (by default it's set to white)
>
> Then it should wait a half a second and then set the background back
> to white and update the text inside the TextView
>
> Here is my code, it's inside my Activity class:
>
> public synchronized void onClick(View v) {
>                // TODO Auto-generated method stub
>                switch (v.getId()) {
>                case R.id.btnPlay:
>                        showNewDraw();
>                        break;
>                }
>
>        }
>
>        protected void showNewDraw() {
>                textResult.setBackgroundColor(Color.BLACK);
>                textResult.setText("Before sleep");
>
>                try {
>                        Thread.sleep(500);
>                } catch (Exception e) {
>                        textResult.setText("Something wrong");
>                }
>
>                textResult.setBackgroundColor(Color.WHITE);
>                textResult.setText("After sleep");
>        }
>
> The way it works now is that it seems to actually take 1/2 second from
> the time the button is pressed and text is updated, but the background
> is not reset to black and back to white - it always stays white.
>
> Either it is being set to black and instantly reset to white or it's
> just never set to black.
>
> What should I do in order for the "delay" effect to work?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to