D
D
Dmitry2021-07-09 23:20:23
Java
Dmitry, 2021-07-09 23:20:23

How to set the progress bar correctly?

When you click on the button, the number of points increases and the progress bar increases. Reaching, for example, up to 30, the maximum value becomes 60, the minimum 30. - This increase is triggered. The second - when it reaches 60, the minimum becomes 60, the maximum - 80. And here is the problem, the values ​​​​do not change.

Where am I wrong?

The initial values ​​of the progressBar.

protected int exp_up = 0;
    public int getExp_up() {
        return exp_up;
    }
    public void setExp_up(int exp_up) {
        this.exp_up = exp_up;
    }
   progressBar.setMin(0);
   progressBar.setMax(30);


Triggered when a button is pressed.

@RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n")
    public void ClickMe(View v){
      setExp_up(getExp_up()+10);
      progressBar.setProgress(getExp_up());
        exp.setText(""+getExp_up());
        lvl.setText(""+getLvl_up());
          setLvl();
        }

The process of processing the progressBar.

@RequiresApi(api = Build.VERSION_CODES.O)
    public void setLvl() {
        if (progressBar.getProgress() < 30) {
            setLvl_up(0);
            progressBar.setMin(0);
            progressBar.setMax(30);
        } else if (progressBar.getProgress() >= 30 || progressBar.getProgress() <= 60) {
            setLvl_up(1);
            progressBar.setMin(30);
            progressBar.setMax(60);
        } else if (progressBar.getProgress() >= 60 || progressBar.getProgress() <= 80) {
            setLvl_up(2);
            progressBar.setMin(60);
            progressBar.setMax(80);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekseyHunter, 2021-07-11
@Starv

Oh, why such bicycles?? And if you decide to add another 100 levels, will you write a new block of code for each?
Do not use an absolute scale, but a relative one. You have a level system, for each next one you need N-experience + 30. Store in a separate array / HashMap the pairs of values ​​\u200b\u200b"level / required experience, counting from 0", store the current experience in a separate variable, and the current level in another local variable, so as not to count several times.
Let's move on to the progress bar. Set the minimum value - 0, the maximum - 100. And to increase the value of the progress bar, use the formula: current value += experience gained / (required_experience_at_current_level / 100). The required_experience_at_the_current_level is calculated as the difference between the previous level and the current one. If the current progress value is >= 100, set the value to 100 and run the "Congratulations, you have leveled up..." method to increase the level (we reset the progress bar in it). After that, you will need to re-initialize the progress bar - take the current level of experience, subtract max. the value of the previous level and pass the resulting value to the method for increasing progress.
Something like this.
PS How to determine the current level - we turn to the HashMap, filter the values, discarding those that are less than the current experience. From the found ones, we choose the minimum. This will be the current level.
PPS And now the answer to your question. Your signs are wrong, the second condition is constantly met (progress <= 60). Remove the equal sign for the upper limits. Here in the first condition is correct (progress < 30).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question