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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:11:30+00:00 2026-06-15T13:11:30+00:00

In my app targeted for Android 4.0.3, I used to have a Button working

  • 0

In my app targeted for Android 4.0.3, I used to have a Button working as a Switch. Clicking on the button was making its resource change (switch between Play/Pause) and some tasks were performing in the background. The particularity is that when the Button was pressed, I wanted the toggle to be unavailable for a short period of time (to prevent fast switching).

To do so, I created a thread that was waiting for X seconds, like this :

void blockButton(){
    new Thread() {
        public void run(){
            try {
                        synchronized(this){
                             button_ready = false;
                             sleep(5000);
                             button_ready = true;
                        }
                }
                catch(InterruptedException ex){}
            }
        }.start();
    }
}

and basically in my Activity/onClickListener

if(button_ready){
    // Start background stuff
    changeButtonResource();
    button.setEnabled(false);
    blockButton();
    button.setEnabled(true);
}

So far so good, everything was runnin smooth. But I eventually wished to update it to a Switch. The code is pretty much the same (except for the onClickListener that turned into a OnCheckedChangeListener).

But now when I click the button once, clicking it again will allow changing its resource – yet the background task will not start (I tried it with a Vibrator and others already, it only changes the resource).

So I almost have the behaviour my Button used to have, but the resource gets messed up. Any idea would be more than welcome ! 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-06-15T13:11:31+00:00Added an answer on June 15, 2026 at 1:11 pm

    You don’t need to create another thread. View.postDelayed(Runnable) gives you a much easier way to enable buttons (or do other UI actions) after a timeout. Your code should look something like this:

    class ... extends Fragment {
        private final Runnable enabler = new Runnable() { public void run() {
            button.setEnabled(true);
        }};
    
        private View button;
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
            View result = ...
            button = result.findViewById(...);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    button.setEnabled(false);
                    ...
                }
            });
            ...
        }
    
        public void onDestroyView() {
            ...
            button = null;
        }
    
        public void onPause() {
            ...
            button.removeCallbacks(enabler);
        }
    
        public void onResume() {
            ...
            button.setEnabled(true);
        }
    }
    

    Calling removeCallbacks before your Fragment or Activity goes away is important to avoid concurrency problems caused by the user leaving during the disabled period. And after disabling the callback in that case, it’s important to make sure you don’t then get ‘stuck’ in the disabled state when the user comes back to your Fragment or Activity and the View restores its saved state.

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

Sidebar

Related Questions

I have a mobile app that I targeted for iOS and Android. It makes
In my web app targeted to iphone and android, I have a TEXT ME
I have created an app targeted for ios 4.1 with base SDK ios 5.0
All, I'm working on an HTML-based web app (i.e., not native app) targeted for
I have a VS 2008, .NET 3.5 targeted MVC.NET app. I am developing on
The app I am working on is supporting android 2.3 upwards. As the download
I currently have an app on the market that supports Android 1.5 (SDK level
I have an iPad-only app (targeted device family is iPad, architecture is armv7), but
I am working on an app targeted for > iOS 4. In this app
I'm working on a Titanium app (currently targeted for iPhone deployment, but ultimately intended

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.