1

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.

hrk singh
  • 143
  • 12

3 Answers3

1

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();
        }
Amirhosein
  • 4,266
  • 4
  • 22
  • 35
0
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

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • 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 01 '18 at 17:39
0

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


    }
}
hrk singh
  • 143
  • 12