I am trying to set Timer and tried this logic, but it does not run?


I am trying to set Timer and tried this logic, but it does not run?



while running this code, It directly set a text to 0 not decreasing it from 30 to 0 with an interval of 1 second.



JAVA CODE


public void LoadCounter() {

for (int i = 30; i > -1; i--) {
final int counter = i;

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
txtview.setText("" + counter);
Log.d("test1", "" + counter);
}
}, 1000);


}
}



Text view that set value while runtime.



XML CODE


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000"
android:layout_centerInParent="true"
android:textStyle="bold"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtview"
android:layout_centerInParent="true"
android:text="START COUNT"
android:textStyle="bold"
android:textSize="20sp"/>





I know CountdownTimer method is an option but I am trying to solve in this way So
any Changes in above code or explanation is much appreciated.



This is my first time asking a question on StackOverflow and I hope this is a right way to ask and thank you for any help in advance.





You migh like developer.android.com/reference/android/os/CountDownTimer and stackoverflow.com/q/1877417/3166697
– Dima Kozhevin
Jul 1 at 15:53





I can use CountDownTimer method but I would like to know what is wrong with my logic. Why it is not working properly?
– hrk singh
Jul 1 at 17:39





This code creates a lot of Handlers and Runnables immediatelly without any pauses so it works wrong. I'm sorry, but it looks like the invention of a wheel.
– Dima Kozhevin
Jul 1 at 18:02


Handler


Runnable





@DimaKozhevin Thanks a lot. I am using this method only for the learning purpose.
– hrk singh
Jul 1 at 18:20





You are welcome. Thanks for the interesting question.
– Dima Kozhevin
Jul 1 at 18:23




3 Answers
3



and we invent wheel again :)


public void LoadCounter() {
Thread timer = new Thread(){
public void run(){
try{
for (int i = 30; i>= 0; i--){
sleep(1000);
final int timer = i;
runOnUiThread(new Runnable() {
public void run() {
txt.setText(String.valueOf(timer));
}
});
};
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
};
timer.start();
}





Now I know where I went wrong!! thanks :)
– hrk singh
Jul 2 at 5:28


new CountDownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
txtview.setText((millisUntilFinished / 1000) + "");
}

public void onFinish() {
// todo
}

}.start();



https://developer.android.com/reference/android/os/CountDownTimer





I know I can use CountDownTimer method but I would like to know what is wrong with my logic. Why it is not working properly?
– hrk singh
Jul 1 at 17:39



And I figured it out on my own and it's working perfectly.


public void LoadCounter() {

for (int i = 0; i <= 30; i++) {
final int counter = (30 - i);
int TIME = 1000 * i;


new Handler().postDelayed(new Runnable() {
@Override
public void run() {
txtview.setText(String.valueOf(counter));
}
}, TIME);


}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy