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

Age Calculator

This document contains code for an Android application that allows a user to select a date and calculates their age in minutes, hours, and days compared to the current date. It includes code for the MainActivity class, layout files for the user interface, and string/color resources. The MainActivity handles date selection via a DatePickerDialog, performs age calculations on the selected date, and displays the results in TextViews defined in the activity_main layout.

Uploaded by

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

Age Calculator

This document contains code for an Android application that allows a user to select a date and calculates their age in minutes, hours, and days compared to the current date. It includes code for the MainActivity class, layout files for the user interface, and string/color resources. The MainActivity handles date selection via a DatePickerDialog, performs age calculations on the selected date, and displays the results in TextViews defined in the activity_main layout.

Uploaded by

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

MainActivity.

kt

package com.pratham.dob

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.DatePicker
import android.widget.TextView
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

class MainActivity : AppCompatActivity() {


private var tvselecteddate : TextView? = null
private var tvAgeinMinutes : TextView? = null
private var tvAgeinhours : TextView? = null
private var tvAgeindays : TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnDatepicker : Button = findViewById(R.id.button)
tvselecteddate = findViewById(R.id.TextView3)
tvAgeinMinutes = findViewById(R.id.TextView5)
tvAgeinhours = findViewById(R.id.TextView7)
tvAgeindays = findViewById(R.id.TextView9)
btnDatepicker.setOnClickListener {
clickDatePicker()
}
}
private fun clickDatePicker(){
val myCalander = Calendar.getInstance()
val year = myCalander.get(Calendar.YEAR)
val month = myCalander.get(Calendar.MONTH)
val day = myCalander.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this,DatePickerDialog.OnDateSetListener{
view,selectedyear,selectedmonth,selecteddayOfMonth ->
val selectedDate = "$selecteddayOfMonth/${selectedmonth+1}/${selectedyear}"
tvselecteddate?.text = selectedDate
val sdf = SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH)
val theDate = sdf.parse(selectedDate)
theDate?.let { val selectedDateInMinutes = theDate.time / 60000
val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
currentDate?.let { val currentDateInMinutes = currentDate.time / 60000
val differenceInMinutes = currentDateInMinutes - selectedDateInMinutes
val inhours = differenceInMinutes / 60
val indays = inhours / 24
tvAgeinMinutes?.text = differenceInMinutes.toString()
tvAgeinhours?.text = inhours.toString()
tvAgeindays?.text = indays.toString()
}
}
},year,month,day)
dpd.datePicker.maxDate = System.currentTimeMillis() - 86400000
dpd.show()
Toast.makeText(this,"Date Selected",Toast.LENGTH_LONG).show()
}
}

activity_main.xml

<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/bgcolor"
android:gravity="center_horizontal"
>

<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="PRATHAM N GUNDIKERE"
android:textColor="@color/textBlue"
android:textSize="25sp"
android:textStyle="bold"
/>

<TextView
android:id="@+id/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/textBlue"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="AGE CALCULATOR"
android:textColor="#B2EBF2"
android:textSize="25sp"
android:textStyle="bold"
android:textAlignment="center"/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:text="SELECT DATE"
android:textColor="@color/textBlue"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="25sp"
app:cornerRadius="1dp" />
<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="0"
android:textColor="@color/textBlue"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SELECTED DATE"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="0"
android:textColor="@color/textBlue"
android:textSize="45sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="MINUTES"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="0"
android:textColor="@color/textBlue"
android:textSize="45sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="HOURS"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="0"
android:textColor="@color/textBlue"
android:textSize="45sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/TextView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="DAYS"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold"
/>

</LinearLayout>

Colors.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="textBlue">#50858B</color>
<color name="bgcolor">#78CAD2</color>
</resources>

String.xml

<resources>
<string name="app_name">Age In Minutes</string>
</resources>

You might also like