CountDownTimer
CountDownTimer is very useful class provided in android API Level 1.When you want timer,countdown functionality this CountDownTimer comes to help.We can avoid using Threads,TimerTask and Handlers for simple time counting task.CountDownTimer is very easy to use and implement.Also class methods post back results in the UI thread and very light weight class.
Example: Example Bellow shows simple timer that will update countdown every second.CountDownTimer can be easily used in more complex requirement.
Here is Implementing Activity:
import
android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import
android.os.CountDownTimer;
import
android.util.Log;
import
android.view.View;
import
android.widget.TextView;
public class
StartActivity extends Activity
{
private
TextView count_down_text=null;
private
MyCountDownTimer countdowntimer=null;
@Override
protected void
onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.keytalk_countdown_widget);
count_down_text=(TextView)findViewById(R.id.countdown_text);
countdowntimer=new
MyCountDownTimer(10000, 1000);
}
public void
startCountDown(View view)
{
if(!countdowntimer.isRunning())
countdowntimer.startCountDown();
//can also
directly countdowntimer.start()
}
public class
MyCountDownTimer extends CountDownTimer
{
private long starttime;
private boolean isrunning=false;
public
MyCountDownTimer(long startTime, long interval)
{
super(startTime,
interval);
this.starttime=startTime;
}
public void
startCountDown()
{
isrunning=true;
count_down_text.setVisibility(View.VISIBLE);
count_down_text.setText("" + starttime/1000);
Log.d("TAG","
starttime/1000:"+ starttime/1000);
start();
}
@Override
public void
onFinish()
{
count_down_text.setVisibility(View.INVISIBLE);
isrunning=false;
}
@Override
public void
onTick(long millisUntilFinished)
{
count_down_text.setText("" +
millisUntilFinished/1000);
}
public boolean
isRunning()
{
return isrunning;
}
}
}
Layout:
You can use android button class to start the event:
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/application_background">
<com.swapmeen.customcomponent.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bluebutton"
android:text="@string/start_countdown"
android:textColor="#efdd44"
android:textStyle="bold"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="18sp"
android:layout_centerInParent="true"
android:onClick="startCountDown"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countdown_text"
android:text="9"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="200sp"
android:layout_centerInParent="true"
android:visibility="invisible"
/>
</RelativeLayout>
Screenshots of the application:You can use CountDownTimer while implementing splash screens as well.I am coming with few more Easy to use but rarely used android classes which makes developers life easy in my next post stay tuned or subscribe my the blog.
No comments:
Post a Comment