Reputation: 815
My activity opens another activity after 10 seconds. I know how to open the another activity after some time. But what I want to do is also count every second so I can display it to the user. How can I do that? In my activity I have a textview on which I want to show the count down.
public class RestingActivity extends Activity{
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resting);
mHandler.postDelayed(mLaunchTask, 10000);
}
public void skipRestTime(View view)
{
finish();
}
private Runnable mLaunchTask = new Runnable() {
public void run() {
finish(); // Closing the activity
}
};
}
Upvotes: 1
Views: 909
Reputation: 7265
You could use the CountDownAnimation
class. You only need to pass the TextView
in which the count down will be shown and set the CountDownListener
. Then, implement onCountDownEnd
to change the activity.
I use a Handler
, which is much more accurate than CountDownTimer
. When I implemented CountDownAnimation
, I made time tests over those classes.
Upvotes: 1
Reputation: 5604
Please check my answer. Its working fine for me. Hope it should helpful for you. Please try and let me know. Thanks
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:background="#0090FF"
android:padding="10dp"
android:keepScreenOn="true" >
<TextView
android:id="@+id/txtCountDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textColor="#fff" />
</RelativeLayout>
MainActivity.java:
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private static TextView txtCountDown;
private static CountDownTimer countDownTimer = null;
private static final long startTime = 120 * 1000;
private static final long interval = 1 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCountDown = (TextView) findViewById(R.id.txtCountDown);
countDownTimer = new MyCountDownTimer(startTime, interval);
if(txtCountDown!=null){
txtCountDown.setText(String.valueOf(startTime / 1000));
}
countDownTimer.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
if(txtCountDown!=null){
txtCountDown.setText("Time's up!");
}
}
@Override
public void onTick(long millisUntilFinished) {
if(txtCountDown!=null){
txtCountDown.setText("" + millisUntilFinished / 1000);
}
}
}
}
Upvotes: 2