activity_main.xml
<LinearLayout 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:orientation="vertical"> <Button android:id="@+id/startbutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start" android:textAlignment="center"/> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp"> <TableRow> <TextView android:id="@+id/timer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10dp" android:text="Time: " android:textSize="20dp" /> </TableRow> </TableLayout> <TextView android:id="@+id/timeElapsed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="Time elapsed: " android:textSize="20dp" /> </LinearLayout>
MainActivity.java
package in.kantapp.example18; import android.os.Bundle; import android.os.CountDownTimer; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private santimer timer; private Boolean timer_flag=false; private final long startTime = 50000; private final long interval = 1; private Button start; private TextView text,time_eplapsed; private long timeElapsed; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.startbutton); start.setOnClickListener(this); text = (TextView) findViewById(R.id.timer); time_eplapsed = (TextView) findViewById(R.id.timeElapsed); timer = new santimer(startTime, interval); text.setText(text.getText() + String.valueOf(startTime)); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (!timer_flag) { timer.start(); timer_flag = true; start.setText("Start"); } else { timer.cancel(); timer_flag = false; start.setText("Reset"); } } private class santimer extends CountDownTimer { public santimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); // TODO Auto-generated constructor stub } @Override public void onFinish() { // TODO Auto-generated method stub text.setText("Time's up!"); time_eplapsed.setText("Time Elapsed: " + String.valueOf(startTime)); } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub text.setText("Time remain:" + millisUntilFinished); timeElapsed = startTime - millisUntilFinished; time_eplapsed.setText("Time Elapsed: " + String.valueOf(timeElapsed)); } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.kantapp.example18"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
No comments:
Post a Comment