1

I'm writing an android application and I want when httpclient.execute() is being executed, the screen go dark and a progress bar appear until this line is executed, what should I do? my code is like this :

ProgressBar x = (ProgressBar) findViewById("R.id.progress") ;
x.setVisibility(ProgressBar.VISIBLE) ;
httpclient.execute()
x.setVisibility(ProgressBar.GONE) ;

but with this code only the progress bar appears and the screen doesn't become dark.

Navid777
  • 3,591
  • 8
  • 41
  • 69
  • is httpclient an aysnctask? – Broak Jul 16 '13 at 11:49
  • are you sure you want a `ProgressBar` and not a `ProgessDialog`? But you definately need an AsyncTask, [this](http://stackoverflow.com/questions/17527773/displaying-a-progressdialog-while-waiting-for-a-joined-thread) might help you – Benjamin Schwalb Jul 16 '13 at 12:01

3 Answers3

4

Firstly, i recommend that you do all the networking in Async Class. You can use the following template to put your code in Async Class. Take ProgressDialog as a class variable.

YouClass
{
ProgressDialog dialog;

 onCreate(....)
{
  //Execute the async task here.
  new myNetworkingTask().execute("");
}

private class myNetworkingTask extends AsyncTask<String, Void, String> {

  @Override
  protected String doInBackground(String... params) 
        {
            //In this method you will do the networking task
            httpclient.execute();
        }
        return "";
  }      

  @Override
  protected void onPostExecute(String result) { 
          //In this method you will hide the progress bar
         dialog.dismiss();
  }

  @Override
  protected void onPreExecute() {
         //In this method you will display the progress bar.
        dialog = ProgressDialog.show(MyActivity.this, "", 
                    "Loading. Please wait...", true); 
  }

  @Override
  protected void onProgressUpdate(Void... values) {
  }
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
0

If your HTTPClient is in an Asynctask, put your progress bar visibility GONE in the 'onPostExecute'

to dim the screen, you need to show an alert dialog/ dialog fragment and set your custom layout with the progress bar, then dismiss it when the loading as finished.

that will automatically dim the screen.

Broak
  • 4,161
  • 4
  • 31
  • 54
0

Add the httpclient.execute() part inside the asynctas..

Add the pretask inside the preexecute method and post task inside postexecute method.

zacharia
  • 1,083
  • 2
  • 10
  • 22