ArewegoodQ ArewegoodQ - 4 months ago 26
Android Question

Repeated ProgressBar activity for set of tasks in Android

I am new to android, don't have much experience to manage threads. I am working on an activity where I want to display progress bar for say 5 seconds and then repeat. During those 5 seconds, I will display some text for the user to work on the text. I want to repeat this for say N times.

Currently, I have the following code that works for 1 such progress. I tried looping it but it didn't help, as threads executed at same time. How can I repeat this for N number of times? Am I on the correct path in order to solve my the problem?

public class test extends Activity {

private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
progressBar.setScaleY(3f);
// Start long running operation in a background thread

for(int i=0; i<5; i++)
Progress();

}

public void Progress(){
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}

Answer

I am not sure I would recommend using the pattern you are following but here is the way to get the threads to run one after another rather than all at the same time:

public void progress(final int numberOfRuns) {
    if (numberOfRuns <= 0 ) {
        return;
    }
    new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 1;
                // Update the progress bar and display the
                //current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/" + progressBar.getMax());
                    }
                });
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // For the UI Changes. Eg update the loop number
                            myTextView.setText(Integer.toString(totalLoop));
                        }
                    });
                    // Sleep for 200 milliseconds.
                    //Just to display the progress slowly
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            progressStatus = 0;
            totalLoop = totalLoop+1;
            progress(numberOfRuns - 1);
        }
    }).start();
}

then just call progress(numberOfRuns), no need for any loops.

Comments