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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:30:32+00:00 2026-05-23T13:30:32+00:00

I am showing my alert dialog in a separate thread and its not working

  • 0

I am showing my alert dialog in a separate thread and its not working for me. initially when I click

register button for 3000ms I am showing a progress dialogue. and after that I want to show a alert box but its not working. How to solve this?

Thanks in advance…!

 register.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {


Log.v(TAG, "Trying to Login");


   showDialog(0);
   t = new Thread() {
    public void run() {
     showDialog(0);
     try {
        Thread.sleep(3000);
        removeDialog(0);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
   };
   t.start();


  try {

 some data sending to server

 Object responce=(Object)soapEnvelope.getResponse();
   temp=responce.toString();
   if(temp.equals("1")){

 temp = "The result is 1";
         }

      System.out.println("The result is  "+temp);


        new Thread()
          {
             public void run()
             {
                     try
                          {

            sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           AlertDialog.Builder successfullyLogin = new Builder(Register.this);
            successfullyLogin.setCancelable(false);
            successfullyLogin.setMessage("Successfully Login !").show();
        //Toast.makeText(getBaseContext(), "Toast text", Toast.LENGTH_LONG).show();
               }
          }; 



          } catch (Exception e) {

          e.printStackTrace();
           }

             }

             });


               }

           @Override
            protected Dialog onCreateDialog(int id) {
             switch (id) {
              case 0: {
               dialog = new ProgressDialog(this);
              dialog.setMessage("Please wait while connecting...");
             dialog.setIndeterminate(true);
             dialog.setCancelable(true);

           }


             return dialog;
               }

            return null;
                 }
  • 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-23T13:30:33+00:00Added an answer on May 23, 2026 at 1:30 pm

    Replace all your threading with AsyncTask classes. They are designed specifically for this type of thing, and work perfectly with the UI for showing dialogs, and dismissing them in the UI thread while still doing background work where you need it.

    In this way, you don’t need the 3000ms timeout, it just dismisses when it returns. Of course, you could time how long the login takes and keep the dialog up until your 3000ms is up if you want to, but I wouldn’t. Also, if you want to pause in Android use SystemClock.sleep(3000); instead of java’s native thread sleep, as you don’t need to try/catch the interrupt.

    An example that replaces your code (notice the complete lack of threads, try/catches etc that usually litter threading code):

        // ... initialising onClickListener of your button to call the async task
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new StartLoginAsyncTask(YOURAPP.this).execute((Void []) null);
            }
        });
    }
    
    private class StartLoginAsyncTask extends AsyncTask<Void, Void, Integer> {
        private ProgressDialog dialog;
        private final Context context;
    
        public StartLoginAsyncTask(Context context) {
            this.context = context;
        }
    
        @Override
        protected void onPreExecute() {
            // UI work allowed here
            dialog = new ProgressDialog(context);
            // setup your dialog here
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setMessage(context.getString(R.string.please_wait_message));
            dialog.setCancelable(false);
            dialog.show();
        }
    
        @Override
        protected Integer doInBackground(Void... ignored) {
            Integer returnCode = doLogin();
            return returnCode;
        }
    
        @Override
        protected void onPostExecute(Integer returnCode) {
            // UI work allowed here
            dialog.dismiss();
            if (returnCode == LOGIN_OK) {
                // ... show other dialogs here that it was OK
            } else {
                // ... bad news dialog here
            }
        }
    }
    
    private Integer doLogin() {
        // ... write your login code here. 
        // This is run in background, do not do any UI work here
        return LOGIN_OK;
    }
    

    If you want the login to interrupt the dialog, then add a custom TimeoutException to the doLogin(), catch it in the doInBackground(), and return the appropriate Integer response and handle it in onPostExecute().

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

Sidebar

Related Questions

AlertDialog not showing send button. Below is the code. Please tell me what mistake
Hi all I am creating an alert dialog in android by clicking a button.
I want to display alert dialogue on click of pdfimage, I try following code
I have an application where I am showing an alert message using UIAlertView. By
I want to display script errors in a popup alert instead of showing them
while showing progress bar i want to disable touch screen to restrict other functionalities
My dropdown boxes are showing through a jquery modal popup dialog, how do I
Is it possible to have a multi-line title in an Android alert dialog? I
I am showing an alert on UILongPressGestureRecognizer, the issue I am facing is that
I have a question about showing an alert on the screen. The thing is:

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.