Open In App

How to Build a Pomodoro App in Android?

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down work into intervals, traditionally 25 minutes in length, separated by short breaks of 5 minutes. These intervals are known as “pomodoros”. The method is based on the idea that frequent breaks can improve mental agility. The Pomodoro Technique can help you to stay focused and avoid burnout by taking regular breaks. It can also help you to track the amount of time you spend on tasks, which can be helpful for time management and productivity. A sample video is given below to get an idea about what we are going to do in this article.

In this article, we will learn how we can build a basic pomodoro app using android studio and Kotlin language.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    <TextView
        android:id="@+id/timer_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:layout_gravity="center"
        android:text="25:00"
        android:gravity="center" />
  
    <Button
        android:id="@+id/start_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@color/green"
        android:layout_marginTop="5dp"
        android:text="Start" />
    </LinearLayout>
  
</RelativeLayout>


Step 3: Set Timer in TextView 

In order to set the timer in the text view we will use the concept of CountDownTimer. This code includes a CountDownTimer that tracks the time remaining and updates the UI accordingly. The start button has an on-click listener that starts and pauses the timer as needed.

Kotlin




class MainActivity : AppCompatActivity() {
    private var pomodoroTimer: CountDownTimer? = null
    private var timerRunning = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        start_button.setOnClickListener{
            if (!timerRunning) {
                startTimer()
                timerRunning = true
                start_button.text = "Pause"
            
          else {
                pomodoroTimer?.cancel()
                timerRunning = false
                start_button.text = "Start"
            }
        }
    }


Step 4: Write Code for the SeTimer function

To update the UI as the timer counts down, you’ll need to override the onTick and onFinish methods of the CountDownTimer. In the onTick method, you can update the text view to show the time remaining. In the onFinish method, you can handle the case where the timer finishes, either by starting a new timer or taking some other action (such as displaying a notification or playing a sound). We will be showing an alert dialog to start with 5 minutes break and notification after every 25 minutes.

Kotlin




private fun startTimer() {
        val timerLength  : Long = 25 * 60 * 1000
        pomodoroTimer = object : CountDownTimer(timerLength,1000){
            override fun onTick(millisUntilFinished: Long) {
                val minutes = millisUntilFinished / 1000 / 60
                val seconds = millisUntilFinished / 1000 % 60
                timer_text_view.text = "$minutes:$seconds"
            }
  
            override fun onFinish() {
                pomodoroTimer?.cancel()
                timerRunning = false
                // Display the dialog
                val builder = AlertDialog.Builder(this@MainActivity)
                builder.setMessage("Start a 5-minute break timer?")
                    .setPositiveButton("Yes") { _, _ -> startBreakTimer() }
                    .setNegativeButton("No") { _, _ ->
  
                        startTimer()
                        timerRunning = true
                        start_button.text = "Pause"
                    }
                    .show()
  
                    // Create the notification
                    showNotification()
  
            }
  
        }
        pomodoroTimer?.start()
    }


Step 5: Show Notification 

Kotlin




private fun showNotification() {
        val builder = NotificationCompat.Builder(this@MainActivity, "pomodoro_channel")
            .setSmallIcon(R.drawable.ic_baseline_notifications_24)
            .setContentTitle("Pomodoro Timer")
            .setContentText("Your Pomodoro session is finished!")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
  
        // Display the notification
        val notificationManager = NotificationManagerCompat.from(this@MainActivity)
        notificationManager.notify(0, builder.build())
    }


Step 6: Start Break Timer

This function represents 5-minute break duration.

Kotlin




private fun startBreakTimer() {
        val timerLength : Long = 5 * 60 * 1000
        pomodoroTimer = object : CountDownTimer(timerLength, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val minutes = millisUntilFinished / 1000 / 60
                val seconds = millisUntilFinished / 1000 % 60
                timer_text_view.text = "$minutes:$seconds"
  
            }
            override fun onFinish() {
                pomodoroTimer?.cancel()
                timerRunning = false
                val builder = AlertDialog.Builder(this@MainActivity)
                builder.setMessage("Start a 25-minute Pomodoro timer?")
                    .setPositiveButton("Yes") { _, _ -> startTimer() }
                    .setNegativeButton("No") { _, _ ->
                        Toast.makeText(this@MainActivity, "Thank you for using the Pomodoro timer!", Toast.LENGTH_SHORT).show()
                    }
                    .show()
                            }
        }
        pomodoroTimer?.start()
    }


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads