0

I want to create a dynamic intermittent progress bar (as illustrated in the image below), that will change its intervals depending on the user's choice, in Android Studio.

The idea is that the user will choose how many times he wishes to do a behavior, and then the bar will fragment accordingly. Then each time they do the behavior, the bar will color a step increment, as shown in the image below.

Intermittent Progress Bar

I am looking for some general guidance on how to do it, since I am new to this.

I have thought of 3 ways to do this:

  1. Have a ton of png. drawables or vectors for each case, and use one accordingly in an Image View. (seems kind of stupid to me)

  2. Create as many views as are the intervals, and then change the view colors accordingly (in this case there will be a problem with the dynamic part of it i.e. interval variability)

  3. Customize somehow a horizontal ProgressBar to do this automatically.

I have searched the internet for the third way which is the most elegant to me, but cant find an answer.

Thank you for your time.

Theseus
  • 33
  • 9

1 Answers1

0

This is actually trivial to obtain using the current ProgressBar APIs.

Depending on the number of tasks required and number of tasks done, you can update a ProgressBar using setMax and setProgress

progressBar.setMax(totalTasks);
progressBar.setProgress(tasksDone);

This will cover a fraction tasksDone / totalTasks in the progressBar.


Edit: Based on the scenario highlighted in the comments, you can simply use multiple views in a LinearLayout.

You can use a LinearLayout with the background of the partition color you want.

<LinearLayout
    ....
    android:orientation="horizontal"
    android:background="@color/partition"
    ....
/>

And then simply add the child views programmatically with equal weights (and a horizontal margin):-

for(task in tasks) {
    val progressBar = View(this)
    progressBar.marginEnd = gapRequired
    // customize your view here: Set background, shape etc.

    // Set the width to 0 and weight to 1f
    val params = LinearLayout.LayoutParams(0, height, 1f)
    linearLayout.addView(view, index, params)
}

Later to modify an individual view (fragment of progress bar):-

val view = linearLayout.getChildAt(index)
// Modify the view as needed

To remove a view from the layout:-

linearLayout.removeViewAt(index)

Or if you have the view referenced:-

linearLayout.removeView(view)

You can also remove all views (if you need to reset the entire progress bar for some reason) using linearLayout.removeAllViews()

You might want to use RecyclerView with an adapter if you are expecting a lot of these fragments in your progressBar.

Yashovardhan99
  • 887
  • 11
  • 26
  • I want the fragmentation of the ProgressBar to be illustrated, so that the user knows how many fragments they need to complete. I uploaded a new image that shows what I am trying to do in exaggeration (4 tasks, 1 completed in green, 3 remaining in grey). Its this illustrated fragmentation that I haven't found a way to do. – Theseus Jun 16 '21 at 05:18
  • @Theseus For that, you can try using multiple `ProgressBar`s or simply use a rectangular view with a `LinearLayout`. You can have the LinearLayout background set to a white/light grey color (which is used to indicate the partition) and have a bit of margin (say 1dp) between each view to show the partitions (background). Each view can then have a colour set depending on the no. of tasks completed (1st green/ other 3 grey) – Yashovardhan99 Jun 16 '21 at 17:01
  • @Theseus I have updated my answer based on your requirements. (Please accept this answer if it solves the issue) – Yashovardhan99 Jun 16 '21 at 17:24
  • @ Yashovardhan99 Thank you for your answer. Unfortunately I cant know if it solves the issue, since I am new to programming and I use Java, not Kotlin. I understand that you are using in essence the second way that I mentioned in the Post. I will try to do this in Java, and respond when I succeed with the solution. Thanks again for your effort. – Theseus Jun 17 '21 at 15:02
  • @Theseus Should be pretty similar to code in Java (most of the code will still be the same). Reading your question again, this does seem like the 2nd method you suggested. Right now, I can't think of anything else save for building/using a custom view to help solve this issue. Even if you use a custom view, the essence of what's happening inside will be similar to this answer. – Yashovardhan99 Jun 18 '21 at 16:46