Tuesday 3 February 2015

Customized Count down timer


      CountDownTimer is a predefined class, which help us in the timer related activities, such as showing up the  reducing of time seconds, that we have seen while playing games or doing some timer related stuffs to our apps etc… its easy to develop just passing the milliseconds as parameters and it throws as our customized output as (HH:MM:SS) etc… 

1.     Design a layout with a counter start Button and TextView to display the time

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3:    android:orientation="vertical"  
4:    android:layout_width="fill_parent"  
5:    android:layout_height="fill_parent"  
6:    >  
7:  <TextView   
8:    android:id="@+id/countdown"  
9:    android:layout_width="fill_parent"   
10:    android:layout_height="wrap_content"   
11:    android:text="00:00:00"  
12:    />  
13:   <Button  
14:     android:id="@+id/TimeBtn"  
15:     android:layout_width="fill_parent"  
16:     android:layout_height="wrap_content"  
17:     android:text="Count down Start!" />  
18:  </LinearLayout>  

2.      POCApp.java with counter down timer

1:  package com.app.poc;  
2:  import java.text.SimpleDateFormat;  
3:  import java.util.TimeZone;  
4:  import android.app.Activity;  
5:  import android.os.Bundle;  
6:  import android.os.CountDownTimer;  
7:  import android.util.Log;  
8:  import android.view.View;  
9:  import android.widget.Button;  
10:  import android.widget.TextView;  
11:  public class POCApp extends Activity   
12:  {  
13:    public Button TimeBtn;  
14:    public TextView Time;  
15:    MyCounter timer;  
16:    public void onCreate(Bundle savedInstanceState)   
17:    public void onCreate(Bundle savedInstanceState)   
18:    {  
19:      super.onCreate(savedInstanceState);  
20:      setContentView(R.layout.main);  
21:      init();  
22:      process();  
23:    }  
24:    private void process()   
25:    {  
26:      TimeBtn.setOnClickListener(new View.OnClickListener()  
27:      {  
28:        public void onClick(View arg0)   
29:        {      
30:          if(timer!=null)  
31:           timer.cancel();  
32:          timer = new MyCounter(3800*1000,1000);  
33:          timer.start();                          
34:          //Toast.makeText(POCApp.this, disHour+":"+disMinu+":"+disSec, Toast.LENGTH_LONG).show();  
35:        }  
36:      });  
37:    }  
38:    /**  
39:     *  @author Rajendhiran. E  
40:     *  Jan 16, 20134:46:24 PM  
41:     */  
42:    private void init()   
43:    {  
44:    TimeBtn = (Button) findViewById(R.id.TimeBtn);  
45:      Time = (TextView) findViewById(R.id.countdown);  
46:    }  
47:    /**  
48:     *  @author Rajendhiran. E  
49:     *  Jan 16, 20134:46:25 PM  
50:     */  
51:    class MyCounter extends CountDownTimer  
52:    {  
53:      SimpleDateFormat mSimpleDateFormat;  
54:      public MyCounter(long millisInFuture, long countDownInterval)   
55:      {  
56:        super(millisInFuture, countDownInterval);  
57:        mSimpleDateFormat= new SimpleDateFormat("HH:mm:ss");  
58:        mSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));  
59:      }  
60:      @Override  
61:      public void onFinish()   
62:      {  
63:       Log.d("Timer Completed: ","Completed!");  
64:        Time.setText("00:00:00");        
65:      }  
66:      @Override  
67:      public void onTick(long millisUntilFinished)  
68:      {  
69:       Time.setText(mSimpleDateFormat.format(millisUntilFinished));      
70:      }  
71:     }  

No comments: