Android Studio: Need help animating a progressbar - java

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();

Related

How to constantly update a Text View

I'm trying out making an app using Android Studio and I want to constantly update a text view.
I at first thought I should use a loop but that ended up not even running for some reason.
TextView amount = findViewById(R.id.amountTextView);
final SeekBar seekBar = findViewById(R.id.seekBar);
int p = seekBar.getProgress() + 1;
amount.setText(String.valueOf(p));
});
}
}
With this method it doesn't update the results text view. I would like it to constantly update the text view so it shows what number the seek bar is on.
You will need an event for update the state of SeekBar and TextView, in this case I user a Handler(android.os) for update the views each 100 milliseconds of time, but you can use another event like the load of a web service or another.
In your case, it doesn't update because you just update the state of variable, but never update the state of seekbar, that is the reason it will never increment the current state.
private void startLoopUntilEnd() {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
int progress = seekBar.getProgress();
textView.setText(String.valueOf(progress));
progress++;
seekBar.setProgress(progress);
startLoopUntilEnd();
}
};
if (currentState <seekBar.getMax()){
handler.postDelayed(runnable,100);
}
}
You can call this method from onCreate in case of Activity or onResume in case of Fragment and after it will go until the SeekBar is complete recursively.
If you want to change the max of seekBar, you can set it in xml or in code.
Use onSeekBarChangeListener:
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
amount.setTextprogress);
}
});

progress bar is not updating in android studio

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

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);

How to display Button text after a delay in android

I created new custom Button class, I want to achieve, whenever user go to any activity my generic button want to expand from circle to default width. While expanding I want to hide button text for while until button animation complete.
Please check my below code:
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
fiButton.setText(btnText);
}
},500);
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.expand);
super.startAnimation(animation);
}
Easily by
ViewCompat.animate(fiButton ).setStartDelay(500).alpha(1).setDuration(700).setInterpolator(new DecelerateInterpolator(1.2f)).start();
note that you have to set the fiButton alpha to zero android:alpha="0.0"
in you xml or on create view
this line will animate your view from 0 to 1 in 700 millisecond after 500 millisecond.
You can use an AnimationListener. On finishing the animation you should do setText on the TextView.
private void animateMe(Context context){
final String btnText = this.getText().toString();
final FIButton fiButton = this;
fiButton.setText("");
Animation animation = AnimationUtils.loadAnimation(context, R.anim.expand);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
fiButton.setText("");
}
#Override
public void onAnimationEnd(Animation animation) {
fiButton.setText(btnText);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
super.startAnimation(animation);
}

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();
}

Categories

Resources