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.