1

I do have a button and when this button is clicked I want to do some actions and revert them back, in lets to say 5 secs. Example, when Button A is clicked, TextA.Text becomes "Clicked" for 5 secs, in 5 secs the value of text should come back to its original. Here what I do have, but I feel that it is totally wrong way. The code that do delay:

diff=time2-time1;

            while (diff<5000) {
                //Log.d("Timer is", String.valueOf(diff));
                time2=System.currentTimeMillis();
                diff=time2-time1;
            }

so untill loop is working its simulate delay and after i do what i want. Any advice?

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
Daler
  • 1,205
  • 3
  • 18
  • 39
  • Didnt knew about this, i thought the most answers which will be rated they will be counted as an answer. Thanks for this info, in future I will. – Daler May 30 '12 at 04:56
  • You can go back to _all_ your old questions and accept the best answers there too, not just for future questions! – Rob I May 30 '12 at 05:05
  • Definitely, I will do it right now. – Daler May 30 '12 at 05:12

6 Answers6

7

Use a Handler, something like this:

new Handler().postDelayed(new Runnable() {
    void run() {
        // do something later
    }
}, 5000);

Edit: Note that this solution allows the main thread to continue during the delay - refreshing the GUI, handling activity changes (such as "Home" button) correctly, etc. Using Thread.sleep() does not.

Rob I
  • 5,627
  • 2
  • 21
  • 28
2

You have a method to do that :

Thread.sleep(5000)

It can throw a InterruptedException, so you have to catch it.

tibo
  • 5,326
  • 4
  • 37
  • 53
2

Busy loop is a bad idea - it eats your batteries alive. Perhaps Thread.sleep would work better.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Try this code,

// First save the current value of Textbox in a variable 
String tmpVal = myEditText.getText();
// Now Setting of new Value for 2 seconds 
myEditText.setText ( "Clicked" );
// Sleeping for 5 seconds
try
{
     Thread.sleep(5000);
}
//Now Returning back to your old value
myEditText.setText ( tmpVal );

All the above code goes in to public void onClick(View v) method.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

you can get the view on Onclick and can use

@Override
public void onClick(final View v) {
    if(v.getId() == R.id.button1) {

    if(isClikcalbe ){
isClikcalbe  = false;
      view.postDelayed(new Runnable() {

            @Override
            public void run() {

                // you code 
                 isClikcalbe = true;
            }
        }, 5000);
   }

    }
}

can also use bollean isClikcalbe to stop new click until get unable

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

You might want considering AsyncTasks as a container where you do your job. Additionally, you can call Thread.sleep inside it. After you job is done can ask Handler object to change the caption.

Reason for doing it in AsyncTask instead of just sleeping the main thread is that your application doesn't "freeze". Here's how you could use it:

private class AsyncHttpRequest extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... params) {
            Thread.sleep(5000);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            _jobHandler.sendEmptyMessage(0);
        }
    }

protected Handler _jobHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            // set the caption here
        }
    };
user1384991
  • 2,559
  • 3
  • 17
  • 20