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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:46:46+00:00 2026-06-12T23:46:46+00:00

I have an Androd application that talks to a back-end web service. As part

  • 0

I have an Androd application that talks to a back-end web service. As part of the testing, I need to simulate different lengths of client-side delay when a submit button is pressed.

Right now, I am using a Thread.sleep() in my OnClickListener for my submit button, but sleeping the UI thread like this has the unintended consequence of causing the app to appear to “freeze” completely – the button stays highlighted until the thread wakes up and my ProgressBar is likewise completely frozen. Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checkout);

    pBar = (ProgressBar) findViewById(R.id.progressBar1);
    pBar.setVisibility(View.INVISIBLE);

    Button submitButton = (Button) findViewById(R.id.btnSubmit);
    submitButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            pBar.setVisibility(View.VISIBLE);
            try {
                Thread.sleep(3000); //eventually this delay will be random
                                    //right now it obviously hangs the UI
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            performMyAppSpecificWorkHere();
            pBar.setVisibility(View.INVISIBLE);
            CheckoutActivity.this.finish();
        }
    });  
}

I’m looking for ways I could simulate a delay without hanging the entire UI. Any ideas are welcome. Thanks!

edit

Some clarification here! Part of what I need to do is make simulate a delay and allow the user interface to handle it cleanly (which is what’s NOT happening now).

ie…if there is a delay on either the client or the web service, the client should handle it by letting the spinner spin while it waits. The ability to simulate a back-end delay is already done and that works the way I want it to, but I want to simulate a delay on the client without hanging the UI thread.

  • 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-12T23:46:47+00:00Added an answer on June 12, 2026 at 11:46 pm

    here we go:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout);
    
        pBar = (ProgressBar) findViewById(R.id.progressBar1);
        pBar.setVisibility(View.INVISIBLE);
    
        Button submitButton = (Button) findViewById(R.id.btnSubmit);
        submitButton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                Thread t=new Thread(new Runnable(){
                   @Override
                   public void run(){
                     try {
                      Thread.sleep(3000);
                     } catch (InterruptedException e) {
                       e.printStackTrace();
                     }
                     performMyAppSpecificWorkHere();
                     runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pBar.setVisibility(View.INVISIBLE);
                            CheckoutActivity.this.finish();
                        }
                     }
                   }
                });
                pBar.setVisibility(View.VISIBLE);
                t.start();
            }
        });  
    }
    

    So, you are starting a new thread in the onClick event. This new thread should do the application work after sleeping for 3 seconds. After that, you want to update some ui objects, and you can not do that from inside a thread that isn’t the UI Thread. That’s why there is a runOnUiThread(..) after performing the work. Hope it helps!

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

Sidebar

Related Questions

I have a Web Service and an Android application that uses this web service.
I have an android application that consists of a WebWiew and I need to
I have an android application that launches a child Activity under certain situations. Both
I have an Android application that connects to Facebook to request authorization of an
I have an Android application that uses the Android database to create tables and
I have an android application that displays a toast every hour and then every
I have an android application that requres a login based on users in a
I have an android application that has 3 activites. For the 1st and 2nd
I have created an android application that calls (using kSOAP library) a SOAP based
I have a simple Android application that uses a JAR I have built. When

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.