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 7488067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:43:41+00:00 2026-05-29T14:43:41+00:00

I want to implement a button that counts down when pressed. The idea is

  • 0

I want to implement a button that counts down when pressed. The idea is to allow the user to cancel the activation of the button. Once pressed, I would like for the button to change to red and read “CANCEL (3)” and countdown to “CANCEL (0)” and if it is pressed again, the countdown will stop and nothing will happen. If it is not canceled, the app will go to the next screen after time expires. Is there a way to update a button’s text each second (for the countdown) and change its functionality?

Thanks

  • 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-29T14:43:42+00:00Added an answer on May 29, 2026 at 2:43 pm

    You can use postDelayed on a Handler to invoke your method later on the UI thread so that you can update the UI.

    There’s actually a very nice article about how this is done on Android already: http://developer.android.com/resources/articles/timed-ui-updates.html

    You can get a Handler either by creating one in code running on the UI thread, or by calling getHandler() on a view (e.g. your button itself).

    I’d provide an example, but the article linked already does so with great detail and clarity, so I’ll defer to it.

    Edit: here’s a rough overview of how this will look. I don’t have the Android SDK installed right now, so I can’t verify that this works.

    public class CountdownButton extends Button {
      private boolean isCounting = false;
      private int count;
    
      private final Runnable countdownRunnable = new Runnable() {
        @Override
        public void run() {
          setText(Integer.toString(count));
          if (count > 0) {
            count--;
            getHandler().postDelayed(countdownRunnable, 1000);
          } else {
            // count reached zero
            isCounting = false;
            setText("boom");
          }
        }
      }
    
      private final View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          if (isCounting) {
            // stop counting
            isCounting = false;
            getHandler().removeCallbacks(countdownRunnable);
            setText("cancelled");
          } else {
            // start counting
            isCounting = true;
            count = 10;
            countdownRunnable.run();
          }
        }
      }
    
      public CountdownButton(Context context) {
        super(context);
        setOnClickListener(onClickListener);
      }
    
      public CountdownButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(onClickListener);
      }
    
      public CountdownButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setOnClickListener(onClickListener);
      }
    }
    

    The timing may not be perfect (especially if there’s a lot of other CPU work going on), but it should be good enough for your purposes.

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

Sidebar

Related Questions

I want to implement a drop-down list in an iPhone application, the same like
I want to implement the back button functionality so that upon clicking that the
I'm developing a custom control that doesn't inherit from Button. I want to implement
I want to implement a facebook like login on my app. The idea is
I want to implement a long press on my button and after that long
I want to implement a simple volume up/down button using a custom bitmap, and
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running
I want to implement a utility for myself that should provide a small console
I have a button that I only want enabled when a textfield is not

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.