Output Screen:
We need to create:
- Activity
- MainActivity.java: contains java file and xml layout file
- MainActivity.java
- activity_main.xml
Source Codes:
activity_main.xml
<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/tvHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="@string/countdown_timer" android:textSize="24sp"/> <LinearLayout android:id="@+id/llSetTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tvHeader" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:orientation="horizontal"> <EditText android:id="@+id/edtSetTimer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/initialTime" android:inputType="number" android:textSize="40sp" android:maxLength="4" android:gravity="end"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/second" android:textSize="20sp" /> </LinearLayout> <TextView android:id="@+id/tvRemainingTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/llSetTimer" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="@string/time_remaining" android:textSize="24sp"/> <LinearLayout android:id="@+id/llRemainingTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tvRemainingTime" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:id="@+id/tvSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/initialTime" android:textSize="60sp"/> <TextView android:id="@+id/tvSeparator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:text="@string/timeSeparator" android:textSize="60sp"/> <TextView android:id="@+id/tvMilliSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/initialTime" android:textSize="60sp"/> </LinearLayout> <TextView android:id="@+id/tvTimeUp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/llRemainingTime" android:layout_marginTop="10dp" android:layout_centerHorizontal="true" android:textSize="30sp" android:text="@string/timeUp" android:visibility="invisible"/> <Button android:id="@+id/btnStart" android:layout_width="200dp" android:layout_height="80dp" android:layout_below="@+id/tvTimeUp" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="Start" android:textSize="20sp"/> </RelativeLayout>
MainActivity.java
package com.sonevalley.sushil.countdowntimer; import android.os.Bundle; import android.os.CountDownTimer; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText edtSetTimer; TextView tvSecond; TextView tvMilliSecond; TextView tvTimeUp; Button btnStart; CountDownTimer countDownTimer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtSetTimer = (EditText) findViewById(R.id.edtSetTimer); tvSecond = (TextView) findViewById(R.id.tvSecond); tvMilliSecond = (TextView) findViewById(R.id.tvMilliSecond); tvTimeUp = (TextView) findViewById(R.id.tvTimeUp); btnStart = (Button) findViewById(R.id.btnStart); btnStart.setOnClickListener(this); edtSetTimer.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String sec = edtSetTimer.getText().toString(); if (sec != null && sec != "" && sec.length() > 0) { tvSecond.setText(edtSetTimer.getText()); tvMilliSecond.setText("0"); tvTimeUp.setVisibility(View.INVISIBLE); tvSecond.setText("0"); } } @Override public void afterTextChanged(Editable s) { } }); } @Override public void onClick(View v) { if (edtSetTimer.getText().toString().trim().length() != 0) { int startTime = Integer.parseInt(edtSetTimer.getText().toString()) * 1000; Log.i("startTime", startTime + btnStart.getText().toString()); if (v == btnStart && startTime > 0) { if (btnStart.getText().toString().equalsIgnoreCase("START")) { Log.i("startTime Start", btnStart.getText().toString()); countDownTimer = new MyCountDownTimer(startTime, 10); btnStart.setText("STOP"); tvTimeUp.setVisibility(View.INVISIBLE); edtSetTimer.setEnabled(false); countDownTimer.start(); } else { Log.i("startTime Stop", btnStart.getText().toString()); btnStart.setText("START"); edtSetTimer.setEnabled(true); countDownTimer.cancel(); } } } } class MyCountDownTimer extends CountDownTimer { public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long millisUntilFinished) { long milliSecond = millisUntilFinished % 1000; tvMilliSecond.setText(String.valueOf(milliSecond / 100)); long second = millisUntilFinished / 1000; tvSecond.setText(String.valueOf(second)); } @Override public void onFinish() { btnStart.setText("START"); edtSetTimer.setEnabled(true); tvTimeUp.setVisibility(View.VISIBLE); } } }
Reference
http://developer.android.com/reference/android/os/CountDownTimer.html
0 comments:
Post a Comment