progress bar is not updating in android studio - java

i'm using progress bar with countdown timer, everything is working fine except progress bar animation it not working in animation form, its just updating its state after the duration not in animation. Please somebody help me for this problem.
Here is my code
countDownTimer = new CountDownTimer(oneMin,1000) {
#Override
public void onTick(final long millisUntilFinished)
{
/* long finishedSeconds = oneMin - millisUntilFinished;
int total = (int) (((float)finishedSeconds / (float)oneMin) * 100.0);
progressBar.setProgress(total);*/
progressBar.setMax(100);
progress = 1000*60/10;
progressBar.setProgress(progress);
}
#Override
public void onFinish() {
//t1.setText("");
Toast.makeText(getActivity(), "your score", Toast.LENGTH_SHORT).show();
dia1();
progressBar.setProgress(0);
}
};
tt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ans.equals("1"))
{
countDownTimer.cancel();
mp.start();
Random r = new Random();
i1 = r.nextInt(10 - 1) + 1;
colorDetails(i1);
i++;
tt5.setText(String.valueOf("Score:"+i));
countDownTimer.start();
}

You setMax your progress bar 100 and then you setProgress 6000 !! which is over the max value
Your progress bar will stop at the same progress every time you call onTick method because that method update the same value evey time you call it
Read this to read some examples
about progress bar

Related

Android Studio: Need help animating a progressbar

I have a round progress bar, made with a drawable, with values from 0-100 (included). When I load my activity, I want to animate the progressbar being "filled" with values from 0 to my desidered value. I'm using a CountdownTimer for that.
The issue is, even though I set the value to 100, the max (or any other value), the progressbar doesn't get filled all the way. Here's my code:
seekbar = (ProgressBar) findViewById(R.id.progressBar);
seekbar.setMax(100);
final int desiredvalue=100;
CountDownTimer countDownTimer =
new CountDownTimer(1500, 20) {
int startingvalue=0;
public void onTick(long millisUntilFinished) {
float progressPercentage = (desiredvalue*20)/1500;
seekbar.setProgress(startingvalue+=progressPercentage);
}
#Override
public void onFinish() {
}
};
countDownTimer.start();
}
This is what's happening: Open me!
As i understood you want set progress value based on your requirement.
You can use ValueAnimator something like this :
progressBar = findViewById(R.id.my_pb);
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator updatedAnimation) {
Integer animatedValue = Math.round( (float)updatedAnimation.getAnimatedValue());
progressBar.setProgress(animatedValue);
}
});
animation.start();
For more Detail About ValueAnimator
I think just a small modification to your code will fix this. Just change your onTick code like the following.
seekbar = (ProgressBar) findViewById(R.id.progressBar);
seekbar.setMax(100);
final int desiredvalue=100;
CountDownTimer countDownTimer = new CountDownTimer(1500, 20) {
public void onTick(long millisUntilFinished) {
float progressPercentage = (desiredvalue*(1500L-millisUntilFinished))/1500;
seekbar.setProgress(progressPercentage);
}
#Override
public void onFinish() {
}
};
countDownTimer.start();

Flashing/blinking TextView after CountDownTimer ends

I have an app that creates questions (sums) for the user that includes a timer. Once the timer reaches zero, the score is displayed to the user as well as a TextView that says Play Again?
The code runs fine until I implement the flashing/blinking (animation sets TextView color and then transparent). Once the Play Again? is clicked, the app stops but doesn't crash. I think it is because the playAgain() method isn't called?
I want to keep the object oriented approach which is why I created a separate method for the animation called flashPlay().
Here is the playAgain() code:
public void playAgain (View v) {
score = 0;
numOfSums = 0;
timeTextView.setText("30");
scoreTextView.setText("0 : 0");
resultTextView.setText("");
playAgainButton.setVisibility(View.INVISIBLE);
button0.setEnabled(true);
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
playAgainButton.setEnabled(true);
createQuestion();
textViewTimesUp.setVisibility(View.INVISIBLE);
new CountDownTimer(3100, 1000){
#Override
public void onTick(long millisUntilFinished) {
timeTextView.setText(String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
button0.setEnabled(false);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
playAgainButton.setVisibility(View.VISIBLE);
timeTextView.setText("0");
textViewTimesUp.setVisibility(View.VISIBLE);
flashPlay(); //calling the flashPlay() method.
if (score >= 1 && numOfSums >= 1) {
int percent =((score * 100) / numOfSums);
resultTextView.setText("Score: " + percent + "%");
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
} else {
textViewTimesUp.setText("Time's Up!");
questionTextView.setText("");
}
}
}.start();
}
And here is the flashPlay() code:
public void flashPlay () {
final ObjectAnimator colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
playAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
colorAnim.end();
}
});
}
Can someone show me whats wrong with the code.
Thank you.
Sth like this? You should set the OnClickListener once in onCreate(). In flashPlay() it would have been set again every time the method gets called.
ObjectAnimator colorAnim = null;
onCreate(){
...
playAgainButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
if(colorAnim != null && colorAnim.isStarted()){
colorAnim.end();
//playAgain(v); ?
}
}
});
...
}
...
public void flashPlay () {
colorAnim = ObjectAnimator.ofInt(playAgainButton, "textColor", Color.CYAN, Color.TRANSPARENT);
colorAnim.setDuration(600); //duration of flash
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}
in my app, i just blink my textView with this animation:
public static Animation blinkAnim() {
// Configure your animation properties here
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(550);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(10);
animation.setRepeatMode(Animation.REVERSE);
return animation;
}
You can anim it by doing:
// get the animation
Animation anim = blinkAnim();
// start the animation
myTextView.startAnimation(anim);

Java Android, Countdown Timer and ProgressBar (read first)

I have count down timer which is display by ProgressBar and successfully. But unfortunately there is problems.
The progressBar Start in 1/5 instead 0/5. and Stuck 2 seconds in 4/5 progress
If I recall the Function of Count down Timer before
public void onFinish() {
progresBar.setProgress(100);
}
display will be mess up for a while.
I really miss Unity Engine with Time.deltaTime;
Here is my code:
countDownTimer2 = new CountDownTimer(5000,1000) {
int i = 0;
#Override
public void onTick(long milliSecondUntilFinished) {
i++;
progresBar.setProgress((int) i * 100 / (5000/1000));
}
#Override
public void onFinish() {
progresBar.setProgress(100);
}
}.start();
You are incrementing your i variable before setting your progress. When onTick() is called for the first time, the value of i is 1. Thus progress you are setting is 20 which is equivalent to 1/5. Use this -
#Override
public void onTick(long milliSecondUntilFinished) {
progresBar.setProgress((int) i * 100 / (5000/1000));
i++;
}
Also, you should better use milliSecondUntilFinished to find progress instead of unneccesarily introducing i variable.
For the problem with the display, I highly doubt that you have not canceled the timer already running before calling a new one. If yes, then both timer would be running until older one finishes (till this point, the progress bar will be behaving weirdly).
You can cancel the previous timer as -
countDownTimer2.cancel();
Do this before starting a new one.
Using:
countDownTimer2 = new CountDownTimer(1000,5000) {
int i = 0;
#Override
public void onTick(long milliSecondUntilFinished) {
i++;
progresBar.setProgress((int) i * 100 / (5000/1000));
}
#Override
public void onFinish() {
progresBar.setProgress(100);
}
}.start();

Android Studio Java: how do I force a redraw / display of the textbox during the loop?

I have the following code, which I was hoping would count up to 3 million on the screen.
It compiles and runs, displaying 3,000,000 at the end on the emulator. My question is how do I force a redraw / display of the textbox during the loop please?
/** Called when the activity has become visible. */
#Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
TextView textbox1=(TextView)findViewById(R.id.TextView1);
for(double l=0; l<=3000000; l++){
textbox1.setText("" + l);
}
}
The view is only disabled after onResume has finished running.
You may want to set the text in the textview, update some state in the activity (like an int field), and register some code to run after a while and increment. Look at using Handler, AsyncTask, or other options to defer code.
Here's a quick-and-dirty example with Handler.
final long DELAY_MILLIS = 50;
final Handler handler = new Handler();
int num = 0;
final Runnable runnable = new Runnable() {
public void run() {
if (num >= 3000000) return;
textbox1.setText("" + num);
num++;
// re-register ourself to run in DELAY_MILLIS;
handler.postDelayed(runnable, DELAY_MILLIS);
}
};
TextView textbox1;
protected void onResume() {
// more efficient to look this up once
this.textbox1 = (TextView)findViewById(R.id.TextView1);
runnable.run(); // will register itself to re-run
}
According to this answer the best way to show an incremental value in a TextView is to use ValueAnimator
public void animateTextView(int initialValue, int finalValue, final TextView textview) {
ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue);
valueAnimator.setDuration(1500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
textview.setText(valueAnimator.getAnimatedValue().toString());
}
});
valueAnimator.start();
}

How to add time to a running countdown timer?

Before I start I have looked at lots of threads including:
How to add time to countdown timer?
Android game countdown timer
But I just cant get my timer to work in the way I require. I want the timer to be counting down from say 30 and when and image is pressed (named imageview1 in this case) the timer adds 3 seconds to the timer to give it more time. I know you cannot essentially add the time while its running and you need to cancel and then start a new timer, The code I have so far is :
public void onClick(View v) {
// TODO Auto-generated method stub
//GlobalClass global = new GlobalClass();
Random rand = new Random();
CountDownTimer thetimer = new myTimer(millisInFuture, 1000);
switch(v.getId()) {
case R.id.buttonstart:
btnstart.setVisibility(View.INVISIBLE);
thetimer.start();
break;
case R.id.imageView1:
if (thetimer != null){
thetimer.cancel();
thetimer = new myTimer(countdownPeriod + 3000, 1000).start();
}
break;
with lots of other case references then :
public class myTimer extends CountDownTimer {
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
countdownPeriod=millisUntilFinished;
}
#Override
public void onFinish() {
timedisplay.setText("Timer Finished");
started = false;
btnstart.setVisibility(View.VISIBLE);
}
}
I think the problem is its not cancelling the original timer so the label that shows the timer does some crazy things, like jumping around on different numbers both up and down as there would appear more than 1 class of thetimer. That is even though I have included the line thetimer.cancel(); The timer works fine if I just let it run to 0.
Any help would be great
You should not create your timer as a local in onClick. Instead create it as a global and start it somewhere else (in onCreate perhaps).
What happens with your current code is that whenever onClick is called a new timer is created and you then cancel the new timer - which has no effect on any previously created timer(s).
Try something like this:
public class MyActivity extends Activity {
CountDownTimer thetimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thetimer = new myTimer(millisInFuture, 1000);
}
public void onClick(View v) {
Random rand = new Random();
switch(v.getId()) {
case R.id.buttonstart:
btnstart.setVisibility(View.INVISIBLE);
thetimer.start();
break;
case R.id.imageView1:
if (thetimer != null) {
thetimer.cancel();
thetimer = new myTimer(countdownPeriod + 3000, 1000).start();
}
break;
}
}
}
You will still have to keep track of the global time somewhere - i.e. the countDonwPeriod used to re-create the timer instance when an image is touched - it should probably be extracted from the timer before canceling it.

Categories

Resources