0% found this document useful (0 votes)
12 views4 pages

countdown

The document contains XML layout code for a countdown timer app, featuring a TextView for displaying the countdown, an EditText for user input, and buttons for starting, pausing, and resetting the timer. The MainActivity class implements the countdown functionality using a CountDownTimer, allowing users to input time in seconds and control the timer's state. Additionally, it mentions using Android Nougat (7.0) and updating the Gradle version from 34 to 35.

Uploaded by

logeshwari18a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

countdown

The document contains XML layout code for a countdown timer app, featuring a TextView for displaying the countdown, an EditText for user input, and buttons for starting, pausing, and resetting the timer. The MainActivity class implements the countdown functionality using a CountDownTimer, allowing users to input time in seconds and control the timer's state. Additionally, it mentions using Android Nougat (7.0) and updating the Gradle version from 34 to 35.

Uploaded by

logeshwari18a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Xml code

<?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"
android:background="#121212">

<TextView
android:id="@+id/tvCountdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00"
android:textSize="48sp"
android:textColor="#FFFFFF"
android:layout_marginBottom="30dp"/>

<EditText
android:id="@+id/etInputTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter time (seconds)"
android:inputType="number"
android:textColor="#FFFFFF"
android:background="@android:color/darker_gray"
android:gravity="center"
android:textSize="18sp"
android:padding="10dp"
android:layout_marginBottom="20dp"/>

<Button
android:id="@+id/btnStartPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:padding="10dp"
android:layout_marginBottom="10dp"/>

<Button
android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:padding="10dp"/>

</LinearLayout>

2) Mainact.kt

package com.example.countdownapp

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


private lateinit var tvCountdown: TextView
private lateinit var etInputTime: EditText
private lateinit var btnStartPause: Button
private lateinit var btnReset: Button
private var countDownTimer: CountDownTimer? = null
private var timeLeftInMillis: Long = 0
private var isRunning = false

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvCountdown = findViewById(R.id.tvCountdown)
etInputTime = findViewById(R.id.etInputTime)
btnStartPause = findViewById(R.id.btnStartPause)
btnReset = findViewById(R.id.btnReset)

btnStartPause.setOnClickListener {
if (isRunning) {
pauseTimer()
} else {
val inputTime = etInputTime.text.toString()
if (inputTime.isNotEmpty()) {
startTimer(inputTime.toLong() * 1000)
}
}
}

btnReset.setOnClickListener {
resetTimer()
}
}

private fun startTimer(timeMillis: Long) {


timeLeftInMillis = timeMillis
countDownTimer = object : CountDownTimer(timeLeftInMillis, 1000) {
override fun onTick(millisUntilFinished: Long) {
timeLeftInMillis = millisUntilFinished
updateCountdownText()
}

override fun onFinish() {


isRunning = false
btnStartPause.text = "Start"
tvCountdown.text = "00:00:00"
}
}.start()

isRunning = true
btnStartPause.text = "Pause"
}

private fun pauseTimer() {


countDownTimer?.cancel()
isRunning = false
btnStartPause.text = "Start"
}

private fun resetTimer() {


countDownTimer?.cancel()
isRunning = false
timeLeftInMillis = 0
tvCountdown.text = "00:00:00"
btnStartPause.text = "Start"
etInputTime.text.clear()
}

private fun updateCountdownText() {


val minutes = (timeLeftInMillis / 1000) / 60
val seconds = (timeLeftInMillis / 1000) % 60
tvCountdown.text = String.format("%02d:%02d", minutes, seconds)
}
}

Use naugat 7.0 version and change 34 to 35 in gradle

You might also like