Reputation: 237
I've a horizontal progressbar which shows progress from 0 to 100. What I want is to restart the progressbar again from 0 when it reaches 100. I'll be showing this in a Fragmentdialog. For now I'm checking it in activity itself. How to do that?
public class MainActivity extends AppCompatActivity{
private Button startbtn, stopbtn;
ProgressBar pb;
private TextView progressTxt;
int progressBarValue = 0;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = new CShowProgress(MainActivity.this);
btn = (Button)findViewById(R.id.btn);
startbtn = (Button)findViewById(R.id.startbtn);
stopbtn = (Button)findViewById(R.id.stopbtn);
pb = (ProgressBar)findViewById(R.id.progressBar);
progressTxt = (TextView)findViewById(R.id.progressTxt);
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myprogress(true);
}
});
stopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myprogress(false);
}
});
}
private void myprogress(final Boolean isStart){
handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if(isStart && progressBarValue<100)
{
progressBarValue+=1;
pb.setProgress(progressBarValue);
progressTxt.setText(String.valueOf(progressBarValue));
handler.sendEmptyMessageDelayed(0, 100);
}
}
};
handler.sendEmptyMessage(0);
}
}
Upvotes: 0
Views: 990
Reputation: 62209
I assume this chunk of code will work for your use case:
private void myprogress(final Boolean isStart) {
handler = new Handler() {
public void handleMessage(Message msg) {
if (isStart && progressBarValue < 100) {
...
} else if (isStart) {
// progressBarValue is equal to 100
// make it zero and do the same thing again
progressBarValue = 0;
progressBarValue += 1;
pb.setProgress(progressBarValue);
progressTxt.setText(String.valueOf(progressBarValue));
handler.sendEmptyMessageDelayed(0, 100);
}
}
};
handler.sendEmptyMessage(0);
}
Upvotes: 1
Reputation: 67473
First, you should use a counter mechanizm (run a delayed handler, timer, thread or something else)to check current progress periodically.
(progressTxt.getProgress == 100)
will do the trick.
Another way is using this answer.
Upvotes: 0