impossible
impossible

Reputation: 2500

Create Static Progress Bar in Android

By static I mean, I have some data say 20, whose max value can be 100. I want to name it Progress. Then I need an activity which will be called when we open Progress tab. Where it loads the Progress and stops when it reaches 20 out of 100.

It should finally look like.

Progress.
++++----------------
Progress1.
+++++++++++---------
Progress3.
+++++++-------------

I googled and got this gif which exactly tells my requirement. GIF

What I tried and could achieve is:

activity

public class test extends Activity {
    private ProgressBar progressBar;
    private int progressStatus = 0, CurrValue=20;
    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
        Progress();
    }
    public void Progress(){
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < CurrValue) {
                    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();
    }
}

layout

It has a TextView, and a ProgressBar

I want to show multiple progress bar with the animation just like in the GIF link above.

Upvotes: 0

Views: 1185

Answers (1)

iheanyi
iheanyi

Reputation: 3113

Here's an idea:

  1. Create a ProgressUpdater class. It takes a ProgressBar in its constructor, the variable to monitor for progress, and the max value (assuming 0 is the min, or min value if it's not 0 to max).
  2. It has an UpdateProgress() method that you call to have it check the variable and compute progress/update the ProgressBar.
  3. Your Activity contains an array of ProgressUpdaters which is initialized in onCreate with each ProgressBar.
  4. Progress() then loops through the array of ProgressUpdaters and calls UpdateProgress() on each one. You'd need to make some changes to Progress() to handle multiple values, or instead, use some Listener and have it call the UpdateProgress() method of the appropriate ProgressUpdater.

How does that sound?

Upvotes: 1

Related Questions