Age In Minutes App in Kotlin

Age In Minutes App in Kotlin

An App to calculate how many minutes old are you (Source Code available at the end)

Setting Up the User Interface

  • For Designing the UI of the App we will use Linear Layout

  • Orientation of the Linear Layout will be vertical

  • Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@color/backgroundColor"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="CALCULATE YOUR"
        android:textColor="@color/textColor"
        android:textSize="25sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="AGE"
        android:padding="10dp"
        android:textColor="@color/white"
        android:textSize="25sp"
        android:background="@color/ageTextBackgroundColor"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="IN MINUTES"
        android:textColor="@color/textColor"
        android:textSize="25sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnDatePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#FFFFFF"
        android:textColor="#929292"
        android:text="Select Date"
        android:layout_marginTop="15dp"
        android:textStyle="bold"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tvSelectedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#844046"
        android:layout_marginTop="20dp"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/lightGreyTextColor"
        android:text="Selected Date"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/tvSelectedDateInMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#844046"
        android:layout_marginTop="20dp"
        android:textSize="35sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/lightGreyTextColor"
        android:text="Age in minutes"
        android:textSize="18sp"/>


</LinearLayout>

Writing The Logic Of the App

  • First we will pass the reference the btnDatePicker from the layout and set onclicklistener to btnDatePicker where we will call the "clickDatePicker()" function
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnDatePicker: Button = findViewById(R.id.btnDatePicker)

        btnDatePicker.setOnClickListener {
             //call clickDatePicker when this button is clicked
            clickDatePicker()
        }
    }

Function to open Date Picker Dialogue Box

  • This Gets a calendar using the default time zone and locale. The calender returned is based on the current time in the default time zone with the default.
fun clickDatePicker(){

        val myCalendar = Calendar.getInstance()
        val year = myCalendar.get(Calendar.YEAR)
        val month = myCalendar.get(Calendar.MONTH)
        val day = myCalendar.get(Calendar.DAY_OF_MONTH)

        //get the id of the textviews from the layout
        val tvSelectedDate:TextView = findViewById(R.id.tvSelectedDate)
        val tvSelectedDateInMinutes:TextView = findViewById(R.id.tvSelectedDateInMinutes)

Opening the Date Picker Dialog Box:

  • DatePickerDialog creates a new date picker dialog for the specified date using the parent context's default date picker dialog theme. The listener used to indicate the user has finished selecting a date.
  • Here the selected date is set into format i.e : day/Month/Year, and the month is counted in Kotlin is 0 to 11 so we need to add +1 so it can be as selected.
  val dpd = DatePickerDialog(this,{ _, selectedYear, selectedMonth, selectedDayOfMonth ->

            val selectedDate = "$selectedDayOfMonth/${selectedMonth+1}/$selectedYear"
            // Selected date it set to the TextView to make it visible to user.
            tvSelectedDate.text = selectedDate
  • Here we have taken an instance of Date Formatter as it will format our selected date in the format which we pass it as an parameter and Locale. Here I have passed the format as dd/MM/yyyy.
  • The formatter will parse the selected date in to Date object so we can simply get date in to milliseconds.
    val sdf = SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
    val theDate = sdf.parse(selectedDate)

Formula For Calculating Age in Minutes:

  • Here we have get the time in milliSeconds from Date object and as we know the formula as milliseconds can be converted to second by dividing it by 1000 and the seconds can be converted to minutes by dividing it by 60.
  • Use the safe call operator with .let to avoid app crashing it currentDate is null

  • System.currentTimeMillis() Sets the maximal date supported by this in milliseconds since January 1, 1970 00:00:00 in time zone.

 theDate?.let {

                 val selectedDateInMinutes = theDate.time/60000
                // Here we have parsed the current date with the Date Formatter which is used above.
                val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
                //use the safe call operator with .let to avoid app crashing it currentDate is null
                currentDate?.let {
                    // Current date in to minutes.
                    val currentDateToMinutes = currentDate.time/60000
                    val differenceInMinutes = currentDateToMinutes - selectedDateInMinutes
                    tvSelectedDateInMinutes.text = differenceInMinutes.toString()
                }
            }
        },
        year,month,day)

Restricting the user from selecting today and future day:

  • 86400000 is milliseconds of 24 Hours. Which is used to restrict the user from selecting today and future day.
dpd.datePicker.maxDate = System.currentTimeMillis() - 86400000
 dpd.show()

Output Screenshots

ksnip_20220413-001535.png
image.png

Download Source Codes:

Download the source code of the app from here: github.com/abhinavkr2108/AgeInMinutes