0

I have a problem with indeterminate progress in android. I have a button and on click of the button the indeterminate progress bar should appear and at the end of the task the progress bar should disappear.

But i am unable to do this.

package com.indeterminate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.*;

 public class IndeterminateProgressBarActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    final ProgressBar pb=new ProgressBar(this);
    pb.setVisibility(View.GONE);
    Button b =new Button(this);
    b.setText("Click to start spinning");
    b.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            pb.setVisibility(View.VISIBLE);
            // Want to write some code
            pb.setVisibility(View.GONE);

            // But the progress bar is not shown on the screen

    LinearLayout l=new LinearLayout(this);
    l.addView(pb);
    l.addView(b);

    this.setContentView(l);
}

}

nikhil
  • 9,023
  • 22
  • 55
  • 81

2 Answers2

1

use Async Task for that. in onClick call

new SomeTask(0).execute();

/** Inner class for implementing progress bar before fetching data **/
    private class SomeTask extends AsyncTask<Void, Void, Integer> 
    {
        private ProgressDialog Dialog = new ProgressDialog(yourActivityClass.this);
        @Override
        protected void onPreExecute()
        {

            Dialog.setMessage("Doing something...");
            Dialog.show();
        }

        @Override
        protected Integer doInBackground(Void... params) 
        {
            //Task for doing something 

            return 0;
        }

        @Override
        protected void onPostExecute(Integer result)
            {

            if(result==0)
            {
//do some thing
            }

    // after completed finished the progressbar
            Dialog.dismiss();
        }

    }
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
1

For this concept you should asynctask class

on preExecute u can set the progressBar visible and in DoInBackGround you can write ur code and in PostExecute you can set the visiblity of progressBar Gone.

static class PleaseWait extends AsyncTask<void, void, Void> {

final ProgressBar pb=new ProgressBar(this); pb.setVisibility(View.GONE);

    protected void onPreExecute() {
                 pb.setVisibility(View.VISIBLE);

    }

    protected Void doInBackground(Object... args) {
        //Write some code here
    }

    protected void onPostExecute(Void result) {
        pb.setVisibility(View.GONE);

    }
}

Than u can call new PleaseWait().execute;

Hardik4560
  • 3,202
  • 1
  • 20
  • 31
  • Thank u so much. But it gives me an error when initializing the "final progress bar" – nikhil Nov 11 '11 at 14:50
  • nikhil, You should declare that progress bar as member variable of the outer class or else you can remove the final keyword at declaration. It will work. – Hardik4560 Nov 12 '11 at 05:27