Practical No. 15: Design and develop a simple countdown timer.
Practical No. 15: Design and develop a simple countdown timer.
Objective:
Develop a countdown timer where the user can enter the desired time (in minutes).
The app should display countdown using the CountDownTimer class.
1. Project File Structure (Tree Diagram)
CountdownTimerApp/
│
├── app/
│ ├── src/main/
│ │ ├── java/com/example/countdowntimer/
│ │ │ └── MainActivity.java
│ │ │
│ │ ├── res/
│ │ │ ├── layout/
│ │ │ │ └── activity_main.xml
│ │ │ │
│ │ │ └── values/
│ │ │ └── strings.xml
│ │ │
│ │ └── AndroidManifest.xml
2. activity_main.xml (User Interface)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">
<!-- Student Name -->
<TextView
android:id="@+id/tvStudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/student_name"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<!-- Input Minutes -->
<EditText
android:id="@+id/etMinutes"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Enter minutes"
android:inputType="number"
android:layout_marginBottom="20dp"/>
<!-- Timer Display -->
<TextView
android:id="@+id/tvTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textSize="40sp"
android:textStyle="bold"
android:layout_marginBottom="30dp"/>
<!-- Start Button -->
<Button
android:id="@+id/btnStart"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Start Timer"/>
<!-- Reset Button -->
<Button
android:id="@+id/btnReset"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_marginTop="10dp"/>
</LinearLayout>
3. MainActivity.java (Complete Logic)
package com.example.countdowntimer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView tvTimer;
private EditText etMinutes;
private Button btnStart, btnReset;
private CountDownTimer countDownTimer;
private long timeLeftMillis = 0;
private boolean timerRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTimer = findViewById(R.id.tvTimer);
etMinutes = findViewById(R.id.etMinutes);
btnStart = findViewById(R.id.btnStart);
btnReset = findViewById(R.id.btnReset);
updateTimerText();
btnStart.setOnClickListener(v -> startTimer());
btnReset.setOnClickListener(v -> resetTimer());
}
private void startTimer() {
if (timerRunning) {
Toast.makeText(this, "Timer already running", Toast.LENGTH_SHORT).show();
return;
}
String input = etMinutes.getText().toString().trim();
if (TextUtils.isEmpty(input)) {
Toast.makeText(this, "Please enter minutes", Toast.LENGTH_SHORT).show();
return;
}
int minutes = Integer.parseInt(input);
if (minutes <= 0) {
Toast.makeText(this, "Enter a valid time", Toast.LENGTH_SHORT).show();
return;
}
timeLeftMillis = minutes * 60 * 1000;
countDownTimer = new CountDownTimer(timeLeftMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftMillis = millisUntilFinished;
updateTimerText();
}
@Override
public void onFinish() {
timerRunning = false;
tvTimer.setText("Time's Up!");
}
}.start();
timerRunning = true;
}
private void resetTimer() {
if (countDownTimer != null) {
countDownTimer.cancel();
}
timerRunning = false;
timeLeftMillis = 0;
updateTimerText();
etMinutes.setText("");
}
private void updateTimerText() {
int minutes = (int) (timeLeftMillis / 1000) / 60;
int seconds = (int) (timeLeftMillis / 1000) % 60;
String timeFormatted = String.format(Locale.getDefault(),
"%02d:%02d", minutes, seconds);
tvTimer.setText(timeFormatted);
}
@Override
protected void onPause() {
super.onPause();
if (countDownTimer != null) {
countDownTimer.cancel();
}
timerRunning = false;
}
}
4. strings.xml
<resources>
<string name="app_name">CountdownTimerApp</string>
<string name="student_name">Student Name: FirstName LastName</string>
</resources>
5. AndroidManifest.xml (Check Entry)
<application
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/Theme.CountdownTimerApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
