简体   繁体   中英

Android progressBar not increasing progress

I have a countdown like progressBar and I want to increase its progress on a buttonClick. The problem is that the progress is increasing but only visually. If the progress is down at 60 and I press the button to increment it should be 80, but instead, the progress is decrementing from 60 and not 80. Here's my code:

public class MainActivity extends AppCompatActivity {
    ProgressBar timeLeft;
    CountDownTimer gameCountDownTimer;
    Button increment;
    int progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timeLeft = (ProgressBar) findViewById(R.id.timeLeft);
        increment = (Button) findViewById(R.id.increment);
        timeLeft.setMax(100);
        setTimer(10);

        increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timeLeft.incrementProgressBy(20);
            }
        });
    }

    private void setTimer(int time) {
        progress = 100;
        final int actualTime = time*1000;
        timeLeft.setProgress(progress);
        gameCountDownTimer = new CountDownTimer(actualTime, 1000) {
            int totalTime = actualTime;
            @Override
            public void onTick(long millisUntilFinished) {
                progress = (int)( millisUntilFinished  /(double)totalTime * 100);
                timeLeft.setProgress(progress);
            }
            @Override
            public void onFinish() {
                progress = 0;
                timeLeft.setProgress(progress);
            }
        }.start();
    }
} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Main">


    <ProgressBar
        android:id="@+id/timeLeft"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:progress="100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/increment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Increment"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Instead of using a timer use postDelayed method:

int interval=1000;
int totalTime=10000;
timeLeft.postDelayed(new Runnable() {
    @Override 
    public void run() { 
        currentTime+=interval;
        timeLeft.setProgress(timeLeft.getProgress()-(timeLeft.getMax()/totalTime)*interval);
        if(timeLeft.getProgress>0){
            timeLeft.postDelayed(this,interval);
         }
    } 
}, interval); 

try this progress calculation. for example take millisUntilFinished is 5000 and totalTime is 10000.

then progress should be 50.

in your case it was (int)0.005 which is 0 that's why your progressbar stucked at single point.

@Override
public void onTick(long millisUntilFinished) {
    progress = (int)( (millisUntilFinished* 100 ) /(double)totalTime);
    timeLeft.setProgress(progress);
}

Didn't you miss the fact that timeNow = timeMax - millisUntilFinished ?

This worked for me:

Activity class member:

    lateinit var progressbarView:ProgressBar

Inside onCreate :

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
        progressbarView = findViewById<ProgressBar>(R.id.progressBar)
        // ...
    }

Outside onCreate


    var timeMax:Long = 8000
    var timeInc:Long = 10
    private val timer = object: CountDownTimer(timeMax, timeInc) {
        override fun onTick(millisUntilFinished: Long) {
            val timeNow = timeMax - millisUntilFinished
            val percent: Double = timeNow.toDouble() / timeMax.toDouble() * 100.0
            // By default, the progress bar is full when the progress value reaches 100
            progressbarView.setProgress(percent.toInt())
        }

        override fun onFinish() {
            // ...
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM