Counting up 0 to 100 - java

CountDown 0 to 100 Android
I'm only beginner with android can you help me guys with this.
The Result must be the end of the countdown.
for example, the result value is 75 the countdown will start to 0 ends to 75
mCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResult();
new CountDownTimer(30000, 100){
public void onTick(long millisUntilFinished){
waveLoadingView.setProgressValue(counter);
waveLoadingView.setCenterTitle(String.format("%d%%",result)); //result is the value from randomize 60 to 80 .
result++;
}
public void onFinish(){
}
}.start();
}
});

// result varible define as a public
long result = 80;
mCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new CountDownTimer(result * 1000, 100) {
public void onTick(long millisUntilFinished) {
// its start with 1 if you want start with 0 then replace with below code
//int sec = (int) (result - (millisUntilFinished / 1000)-1);
int sec = (int) (result - (millisUntilFinished / 1000));
waveLoadingView.setProgressValue(sec);
waveLoadingView.setCenterTitle(String.valueOf(result)); //result is the value from randomize 60 to 80 .
}
public void onFinish() {
}
}.start();
}
});

Try below code
mCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResult();
new CountDownTimer(result*1000, 1000){
public void onTick(long millisUntilFinished){
waveLoadingView.setProgressValue(counter);
waveLoadingView.setCenterTitle(String.format("%d%%",newResult)); //newResult is temp value initialize with 0
newResult++;
}
public void onFinish(){
}
}.start();
}
});

You simply need to use the matching parameters when instantiating the CountDownTimer for your use case, take a look for CountDownTimer constructor method CountDownTimer(long millisInFuture, long countDownInterval). When you want to create a count down from 0 to 100 with 1 second step, you can construct your code like this:
final long ONE_SECOND = 1000;
final long ONE_HUNDRED_SECOND = 100 * ONE_SECOND;
new CountDownTimer(ONE_HUNDRED_SECOND, ONE_SECOND){
public void onTick(long millisUntilFinished) {
int second = millisUntilFinished / ONE_SECOND;
int counter = second - 1;
// do something with the counter value
}
public void onFinish() {
// do something when we finished the count down.
}
}.start();

Related

How to implement decreasing counter in textView.setText using for loop?

I'm trying to implement a decreasing counter using for loop in android java. I have used handler & runnable to delay for loop iteration and I want the counter to start from 80 and end on 0, but in output, I'm getting counter from 0 to 80. In short, the reverse is required.
This is my code,
TextView totalpoints = (TextView) findViewById(R.id.txttotalpoints);
Handler handler1 = new Handler();
for (int count = 80;count>=0; count--){
int finalCount = count;
handler1.postDelayed(new Runnable() {
#Override
public void run() {
totalpoints.setText("Total Points : "+ finalCount);
System.out.println("This is in newpointsCounter" + finalCount);
}
}, 1000 * count);
}
Current output => Start from 0 & end on 80
Required output => Start from 80 & end on 0
You can user CountDownTimer as:
CountDownTimer countDownTimer = new CountDownTimer(80000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//TODO on each interval, print your count
}
#Override
public void onFinish() {
//TODO on finish of timer, you will get notified
}
};
countDownTimer.start();
Try this :
final Handler handler = new Handler();
int count = 80;
final Runnable runnable = new Runnable() {
public void run() {
totalpoints.setText("Total Points : " + count);
Log.d(TAG, "count: " + count);
if (count-- > 80) {
handler.postDelayed(this, 5000);
}
}
};
handler.post(runnable);
Also I would recommend using log tags rather than system.println for android
No need to use handler, Android provides CountDownTimer itself just use it.
// For Java
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
System.out.println( millisUntilFinished / 1000)
}
public void onFinish() {
//work done
}
}.start();
// For kotlin
object : CountDownTimer(30000, 1000) {
override fun onTick(millisUntilFinished: Long) {
System.out.println( millisUntilFinished / 1000)
}
override fun onFinish() {
}
}.start()

Making repetitive CountDownTimer with Different Times

I am trying to learn coding. So I decided to make a little project but i stuck.
I am trying to make a CountDownTimer. I have 3 different times. For example first one is 10 sec, second one 5 sec and the third one is 7 sec. So I wanna make an app that start the count from 10 sec and when it finish it start the count from second timer and then third one.
public class MainActivity extends AppCompatActivity {
private Button mStartButton;
private Button mResetButton;
private Button mStopButton;
private TextView mTextViewCountDown;
private TextView mTextViewCounter;
private CountDownTimer mCountDownTimer;
private int countme = 0 ;
private int [] array = new int[3];
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartButton = findViewById(R.id.button_start);
mStopButton = findViewById(R.id.button_stop);
mResetButton = findViewById(R.id.button_reset);
mTextViewCountDown = findViewById(R.id.text_view_countdown);
mTextViewCounter = findViewById(R.id.text_s);
array[0]=10000;
array[1]=5000;
array[2]=70000;
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i<3; i++){
mCountDownTimer = new CountDownTimer(array[i], 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTextViewCountDown.setText(""+ millisUntilFinished/1000);
}
#Override
public void onFinish() {
countme++;
if(countme / 3 == 3){
mCountDownTimer.cancel();
}else{
start();
} } }.start();
}
}
}); } }
I don't think for loop is right for my problem. It does not increase variable i once, it increase in every ontick i guess. As a beginner, I couldn't figure out what should I do.
You do not need for loop try something like this:
private void startCountDowntimer(long millis, int count) {
count ++;
int finalCount = count;
new CountDownTimer(millis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTextViewCountDown.setText(""+ millisUntilFinished/1000);
}
#Override
public void onFinish() {
if (finalCount == 1) {
startCountDowntimer(5000, 1);
} else if (finalCount == 2) {
startCountDowntimer(7000, 2);
} else {
//all finished
}
}
}.start();
}
and on the button click:
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startCountDowntimer(10000, 0);
}
});

i want to make counter time in Android

I create a Android app and I want to make time counter. there are two long variables that it storage long data.
When running second finished, walking second must started and after this process go on four times that must finished.
public void Counter(long runsecond, final long walksecond) {
new CountDownTimer(runsecond, 1000) {
public void onTick(long millisUntilFinished) {
showtext.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
showtext.setText("done!");
new CountDownTimer(walksecond, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
showtext.setText("done!");
}
}.start();
}
}.start();
}
i take in for. only it count once. how can i correct this problem?
I think something like that might satisfy your needs. Number of seconds for both, run and walk counters, is configurable. Repeat rate specifies, how many times should both counters run.
public enum CounterType {
RUN_COUNTER,
WALK_COUNTER
}
private void start() {
int runSeconds = 5;
int walkSeconds = 5;
int repeatRate = 4; // 2 - run, 2 walk
startCount(runSeconds, walkSeconds, repeatRate, RUN_COUNTER);
}
private void startCount(final int runSeconds, final int walkSeconds, final int repeatRate, final CounterType actualCounter) {
if (repeatRate == 0) {
return;
}
int actualSeconds = 0;
if (actualCounter == RUN_COUNTER) {
actualSeconds = runSeconds;
} else if (actualCounter == WALK_COUNTER) {
actualSeconds = walkSeconds;
}
new CountDownTimer(actualSeconds * 1000, 1000) {
#Override public void onTick(long millisUntilFinished) {
Log.d("Counter - onTick", actualCounter.toString() + ": millis remaining: " + millisUntilFinished);
}
#Override public void onFinish() {
Log.d("Counter - onFinish", actualCounter.toString());
CounterType nextCounter = actualCounter == RUN_COUNTER ? WALK_COUNTER : RUN_COUNTER;
startCount(runSeconds, walkSeconds, repeatRate - 1, nextCounter);
}
}.start();
}

Pausing a CountDownTimer whilst another one runs - Android

I have two countdown timers in my program, a longer one (120 sec) and a shorter one (3.5 sec). I want the 120 second timer to be paused whilst the 3.5 second timer is running, and for the longer timer to continue running whenever the 3.5 second timer isn't running. So the program starts with the 120 sec remaining whilst the 3.5 sec one runs, then when the 3.5 sec one runs the 120 sec one will start and only pause when the 3.5 sec one runs again (once users presses enter.) How would I do this?
final CountDownTimer loop = new CountDownTimer(3500, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score++;
}
number.setVisibility(View.VISIBLE);
if (score>=0 && score<=2){
int loadG1 = generateG1.nextInt(89999)+10000;
number.setText(""+loadG1);
}
else if (score>=3 && score<=5){
int loadG1 = generateG1.nextInt(899999)+100000;
number.setText(""+loadG1);
}
else if (score>=6 && score<=9){
int loadG1 = generateG1.nextInt(8999999)+1000000;
number.setText(""+loadG1);
}
else if (score>=10 && score<=14){
int loadG1 = generateG1.nextInt(89999999)+10000000;
number.setText(""+loadG1);
}
else if (score>=15 && score<=20){
int loadG1 = generateG1.nextInt(899999999)+100000000;
number.setText(""+loadG1);
}
else if (score>=21) {
int loadG1 = generateG1.nextInt((int) 8999999999L)+1000000000;
number.setText(""+loadG1);
}
input.getText().clear();
start();
return true;
default:
}
}
return false;
}
});
}
}.start();
new CountDownTimer(120000, 1000) {
#Override
public void onTick (long millisUntilFinished) {
}
#Override
public void onFinish() {
TextView result = (TextView) findViewById(R.id.outcome);
result.setText("Score: "+ score);
TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.GONE);
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.GONE);
loop.cancel();
number.setVisibility(View.GONE);
}
}.start();
I have asked this before, but was not given a valid answer unfortunately. Would be grateful if anyone is capable of answering this question. Please feel free to insert any code that'll help explain your answer. Many thanks in advance.
Ok, I will try to give an example, but no guarantee that this is exactly what you need:
create a global variable and the CountDownTimer objects:
Long remainingTime = 120000L;
ThreePointFiveSecondsTimer mThreePointFiveSecondsTimer;
HundredTwentySecondsTimer mHundredTwentySecondsTimer;
create the 120 seconds timer:
public class HundredTwentySecondsTimer extends CountDownTimer {
public HundredTwentySecondsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}
create the 3.5 seconds timer:
public class ThreePointFiveSecondsTimer extends CountDownTimer {
public ThreePointFiveSecondsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
remainingTime = millisUntilFinished;//set the remaining time
}
#Override
public void onFinish() {
//start the 120 second countdowntimer again
mHundredTwentySecondsTimer = new MyCountDownTimer(remainingTime, 1000);
mHundredTwentySecondsTimer.start();
}
}
start the 120 second timer:
mHundredTwentySecondsTimer = new MyCountDownTimer(remainingTime, 1000);
mHundredTwentySecondsTimer.start();
Then, at any time, you decide to start the 3.5 timer:
mThreePointFiveSecondsTimer = new ThreePointFiveSecondsTimer (3500, 1000);
mThreePointFiveSecondsTimer.start();
mHundredTwentySecondsTimer.cancel();
mHundredTwentySecondsTimer = null;
That´s just the idea behind, but you have to adjust this to your needs. Sorry, but can´t give you all the stuff you need, that will be beyond the frame.
You can try like this (I have not tested but hope it will work)
public class MainActivity extends BaseActivity {
private long remainingTimeForTimer = 0;
private CountDownTimer mCountDownTimer
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start first timer
test1(120 * 1000);
// now on the basis of remaining timer you can cancel current timer and after that second timer you can start that timer with remaining time
if(remainingTimeForTimer > 0)
{
test1(remainingTimeForTimer);
}
}
private void test1(long totalTimerTime)
{
mCountDownTimer = new CountDownTimer(totalTimerTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
remainingTimeForTimer = millisUntilFinished;
}
#Override
public void onFinish() {
//trialCount = 0;
}
};
mCountDownTimer.start();
}
#Override
protected void onDestroy() {
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
super.onDestroy();
}
}
Resolved
I have resolved this issue after trying multiple times. I ended up putting the larger timer in the onFinish of the shorter one and setting the initial time of that longer timer equal to millisUntilFinished. Then I cancel the long timer when the user presses enter and it automatically starts with the updated time whenever the EditText box is displayed.

how to display text in countdowntimer when the thread is sleeping in android

I am trying to create a countdown using the countdown timer. When the thread is sleeping I want the string value to display textview and this to display for 10 seconds before the countdown timer starts again.normally,countdown will go from 0 to 9 continuously. i want to set string value when the thread is sleeping. I am unsure how to get this working. this is my countdowntimer coding, Can anyone help me solve this issue?
public class MyCountDownTimer2 extends CountDownTimer {
public MyCountDownTimer2(long startTime2, long interval2) {
super(startTime2, interval2);
}
#Override
public void onFinish() {
countDownTimer2.start();
}
#Override
public void onTick(long millisUntilFinished) {
text2.setText("" + millisUntilFinished / 10);
}
}
if i press stop button i need to display this string value to countdowntimer textview for 10 seconds this is my string value code
public void stop(){
try {
String val2=text.getText().toString();
int val3 = Integer.parseInt(val2);
int num1,num2,num3,num4,temp;
num1 = val3%10;
temp = val3/10;
String s4 = Integer.toString(num1);
text1.setText(String.valueOf(s1));
}
}
i tried this method my value is updated in my countdown textview but i cannot see that value with in fraction of seconds it has gone i need to display atleast 10 or 15 secs what can i do?
public class MyCountDownTimer1 extends CountDownTimer {
public MyCountDownTimer1(long startTime1, long interval1) {
super(startTime1, interval1);
}
#Override
public void onFinish() {
countDownTimer1.start();
}
#Override
public void onTick(long millisUntilFinished) {
text1.setText("" + millisUntilFinished / 10);
String val2=text.getText().toString();
int val3 = Integer.parseInt(val2);
int num1,num2,num3,num4,temp;
num1 = val3%10;
temp = val3/10;
num2=temp%10;
temp=temp/10;
num3=temp%10;
num4=temp/10;
final String s1 = Integer.toString(num4);
new Thread(){
public void run(){
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
text1.setText(String.valueOf(s1));
}
});
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}

Categories