Hello Friends Today I will write a code for CountDownTimer .
STEP 1: Create a project TimerExample
STEP 2: Write this code in MainActivity
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private long starttime = 3600 * 1000;
private final long interval = 1 * 1000;
private int secs;
private int hours;
private int minutes;
private int seconds;
private long remainingmillisec;
private long total;
private TextView tvtimer;
private Button start,pause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvtimer=(TextView)findViewById(R.id.tv_timer);
start=(Button)findViewById(R.id.buttonstart);
pause=(Button)findViewById(R.id.buttonpause);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("Startime", "KK"+starttime);
countDownTimer = new MyCountDownTimer(starttime, interval);
secs = (int) (starttime / 1000);
hours=secs/3600;
minutes=(secs%(60*60)/60);
seconds=(secs%(60*60)%60);
tvtimer.setText("" + String.format("%02d", hours)+ ":"+ String.format("%02d", minutes) + ":"+ String.format("%02d", seconds));
countDownTimer.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
countDownTimer.cancel();
starttime=remainingmillisec;
Log.d("StoTime", "KK"+starttime);
}
});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long starttime, long interval) {
super(starttime, interval);
}
@Override
public void onFinish() {
tvtimer.setText("Time's up!");
}
public void resume(){}
@Override
public void onTick(long millisUntilFinished) {
remainingmillisec=millisUntilFinished;
secs=(int)(millisUntilFinished/1000);
hours=secs/3600;
minutes=(secs%(60*60)/60);
seconds=(secs%(60*60)%60);
tvtimer.setText("" + String.format("%02d", hours) + ":"+ String.format("%02d", minutes) + ":"+ String.format("%02d", seconds));
}
}
}
STEP 3: Write this code in activity_main xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:text="01:00:00"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonstart"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_timer"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:textStyle="bold"
android:text="Start" />
<Button
android:id="@+id/buttonpause"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/buttonstart"
android:layout_marginTop="29dp"
android:textStyle="bold"
android:text="Pause" />
</RelativeLayout>
STEP 4: Write this code in manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.timerexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ritesh.timerexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run The Project .
No comments:
Post a Comment