Daler
Daler

Reputation: 1205

Simulate delay when button clicked

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?

Upvotes: 1

Views: 1364

Answers (6)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

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

Upvotes: 1

user1384991
user1384991

Reputation: 2629

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
        }
    };

Upvotes: 0

Lucifer
Lucifer

Reputation: 29632

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.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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

Upvotes: 2

Rob I
Rob I

Reputation: 5737

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.

Upvotes: 7

tibo
tibo

Reputation: 5474

You have a method to do that :

Thread.sleep(5000)

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

Upvotes: 2

Related Questions