Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 866543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:49:28+00:00 2026-05-15T09:49:28+00:00

I want to make a pause between two lines of code, Let me explain

  • 0

I want to make a pause between two lines of code, Let me explain a bit:

-> the user clicks a button (a card in fact) and I show it by changing the background of this button:

thisbutton.setBackgroundResource(R.drawable.icon);

-> after let’s say 1 second, I need to go back to the previous state of the button by changing back its background:

thisbutton.setBackgroundResource(R.drawable.defaultcard);

-> I’ve tried to pause the thread between these two lines of code with:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

However, this does not work. Maybe it’s the process and not the Thread that I need to pause?

I’ve also tried (but it doesn’t work):

new Reminder(5);

With this:

public class Reminder {

Timer timer;

        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }

        class RemindTask extends TimerTask {
            public void run() {
                System.out.format("Time's up!%n");
                timer.cancel(); //Terminate the timer thread
            }
        }  
    }

How can I pause/sleep the thread or process?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:49:29+00:00Added an answer on May 15, 2026 at 9:49 am

    One solution to this problem is to use the Handler.postDelayed() method. Some Google training materials suggest the same solution.

    @Override
    public void onClick(View v) {
        my_button.setBackgroundResource(R.drawable.icon);
    
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() {
             @Override 
             public void run() { 
                  my_button.setBackgroundResource(R.drawable.defaultcard); 
             } 
        }, 2000); 
    }
    

    However, some have pointed out that the solution above causes a memory leak because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected.

    A more complex solution that avoids the memory leak subclasses the Handler and Runnable with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:

    private static class MyHandler extends Handler {}
    private final MyHandler mHandler = new MyHandler();
    
    public static class MyRunnable implements Runnable {
        private final WeakReference<Activity> mActivity;
    
        public MyRunnable(Activity activity) {
            mActivity = new WeakReference<>(activity);
        }
    
        @Override
        public void run() {
            Activity activity = mActivity.get();
            if (activity != null) {
                Button btn = (Button) activity.findViewById(R.id.button);
                btn.setBackgroundResource(R.drawable.defaultcard);
            }
        }
    }
    
    private MyRunnable mRunnable = new MyRunnable(this);
    
    public void onClick(View view) {
        my_button.setBackgroundResource(R.drawable.icon);
    
        // Execute the Runnable in 2 seconds
        mHandler.postDelayed(mRunnable, 2000);
    }
    

    Note that the Runnable uses a WeakReference to the Activity, which is necessary in a static class that needs access to the UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
I want make interactive application where user launches it and can do various task
Let's say I want make some of my sources publicly available via my blog
Can anyone please explain what the following code checks for? I can make no
I want it to execute the first part of the code, then make the
I want it to execute the first part of the code, then make the
I want to make one demo application regarding the IPC communication .(Communication between Service).
I'm making a flash game, and I want to make a button. When I
In an activity, I'm trying to make a custom transition between two Activities .
I want to make one project that parse wiki pages and get needed information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.