Advanced Age Calculator App Android Studio Kotlin

Advanced Age Calculator App Android Studio Kotlin

Anย Age Calculatorย Appย allows users to calculate their age inย years, months, weeks, days, hours, minutes, andย seconds.ย To create an age calculator app, you must use aย programming languageย and a development platform, such as Java, Kotlin, and Android Studio.

In this post, I will show you How to create an advanced age calculator app using Android Studio and the Kotlin programming language. Here are the steps you can follow to make thisย app:

  1. Open Android Studio and create a new project usingย File> New > New Project.
  2. ย Select โ€œEmpty Activityโ€ as the template for your project, and give your project a name and a package name.
  3. ย Open theย activity_main.xmlย File in the layout folder and add the necessary UI elements for your app, such as a text field for the user to enter their birth date, a button for calculating the age, and a text view for displaying the result. If you Donโ€™t try this XML code

ย  ย ย <?xml version=โ€1.0โ€ณ encoding=โ€utf-8โ€ณ?>

ย  ย ย <androidx.constraintlayout.widget.ConstraintLayout

ย  ย  ย  ย ย 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:background=โ€#120D12โ€ณ>

ย  ย ย <TextView

ย  ย  ย  ย ย android:id=โ€@+id/textViewโ€

ย  ย  ย  ย ย android:layout_width=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€32dpโ€

ย  ย  ย  ย ย android:text=โ€AGE Calculatorโ€

ย  ย  ย  ย ย android:textColor=โ€#FF0288D1โ€ณ

ย  ย  ย  ย ย android:textStyle=โ€boldโ€

ย  ย  ย  ย ย android:textSize=โ€20spโ€

ย  ย  ย  ย ย android:textAllCaps=โ€trueโ€

ย  ย  ย  ย ย android:shadowColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:shadowRadius=โ€@integer/material_motion_duration_long_1โ€ณ

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toTopOf=โ€parentโ€ />

ย  ย ย <TextView

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:id=โ€@+id/todaysdateโ€

ย  ย  ย  ย ย android:text=โ€Todayโ€™s Dateโ€

ย  ย  ย  ย ย android:textSize=โ€18spโ€

ย  ย  ย  ย ย android:textColor=โ€#FF0288D1โ€ณ

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/textViewโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:id=โ€@+id/distodaydateโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:hint=โ€19/05/2022โ€ณ

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/todaysdateโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย ย <ImageView

ย  ย  ย  ย ย android:id=โ€@+id/calenderโ€

ย  ย  ย  ย ย android:layout_width=โ€40dpโ€

ย  ย  ย  ย ย android:layout_height=โ€40dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€32dpโ€

ย  ย  ย  ย ย android:src=โ€@drawable/ic_calenderโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€@+id/todaysdateโ€

ย  ย  ย  ย ย app:layout_constraintHorizontal_bias=โ€1.0โ€ณ

ย  ย  ย  ย ย app:layout_constraintStart_toEndOf=โ€@+id/distodaydateโ€

ย  ย  ย  ย ย app:layout_constraintTop_toTopOf=โ€@+id/distodaydateโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€5dpโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:id=โ€@+id/dateofbirthโ€

ย  ย  ย  ย ย android:text=โ€Date of Birthโ€

ย  ย  ย  ย ย android:textSize=โ€18spโ€

ย  ย  ย  ย ย android:textColor=โ€#FF0288D1โ€ณ

ย  ย  ย  ย ย android:layout_marginTop=โ€15dpโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/distodaydateโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:id=โ€@+id/disdateofbirthโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:hint=โ€19/05/2022โ€ณ

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/dateofbirthโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย />

ย  ย ย <ImageView

ย  ย  ย  ย ย android:id=โ€@+id/calender2โ€ณ

ย  ย  ย  ย ย android:layout_width=โ€40dpโ€

ย  ย  ย  ย ย android:layout_height=โ€40dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€32dpโ€

ย  ย  ย  ย ย android:src=โ€@drawable/ic_calenderโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€@+id/dateofbirthโ€

ย  ย  ย  ย ย app:layout_constraintHorizontal_bias=โ€1.0โ€ณ

ย  ย  ย  ย ย app:layout_constraintStart_toEndOf=โ€@+id/disdateofbirthโ€

ย  ย  ย  ย ย app:layout_constraintTop_toTopOf=โ€@+id/disdateofbirthโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€5dpโ€/>

ย  ย ย <androidx.appcompat.widget.AppCompatButton

ย  ย  ย  ย ย android:id=โ€@+id/calculatebtnโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:textSize=โ€23spโ€

ย  ย  ย  ย ย android:text=โ€Calculateโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:background=โ€#0288D1โ€ณ

ย  ย  ย  ย ย android:textAllCaps=โ€falseโ€

ย  ย  ย  ย ย android:layout_margin=โ€20dpโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/disdateofbirthโ€ />

ย  ย ย <TextView

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:id=โ€@+id/resultdayโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:hint=โ€Age in Daysโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/calculatebtnโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:id=โ€@+id/resultinhoursโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:hint=โ€Age in Hoursโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/resultdayโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:id=โ€@+id/resultinminโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:hint=โ€Age in Minutesโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/resultinhoursโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย ย <TextView

ย  ย  ย  ย ย android:padding=โ€10dpโ€

ย  ย  ย  ย ย android:id=โ€@+id/resultinsecโ€

ย  ย  ย  ย ย android:layout_width=โ€match_parentโ€

ย  ย  ย  ย ย android:layout_height=โ€wrap_contentโ€

ย  ย  ย  ย ย android:layout_marginStart=โ€10dpโ€

ย  ย  ย  ย ย android:layout_marginEnd=โ€10dpโ€

ย  ย  ย  ย ย android:hint=โ€Age in Secondsโ€

ย  ย  ย  ย ย android:textColor=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textColorHint=โ€@color/whiteโ€

ย  ย  ย  ย ย android:textSize=โ€25spโ€

ย  ย  ย  ย ย android:background=โ€@drawable/bgtextviewโ€

ย  ย  ย  ย ย app:layout_constraintEnd_toEndOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintStart_toStartOf=โ€parentโ€

ย  ย  ย  ย ย app:layout_constraintTop_toBottomOf=โ€@+id/resultinminโ€

ย  ย  ย  ย ย android:layout_marginTop=โ€10dpโ€

ย  ย  ย  ย ย />

ย  ย </androidx.constraintlayout.widget.ConstraintLayout>

Getting Started with Mapbox: A Step-by-Step Implementation Mapbox in Android Studio Kotlin

ย  ย class MainActivity : AppCompatActivity() {

ย  ย  ย  ย ย var calendar:ImageView?=null

ย  ย  ย  ย ย var calendar2:ImageView?=null

ย  ย  ย  ย ย var todaysdate:TextView?=null

ย  ย  ย  ย ย var dob:TextView?=null

ย  ย  ย  ย ย var result:TextView?=null

ย  ย  ย  ย ย var resulthours:TextView?=null

ย  ย  ย  ย ย var resultinmin:TextView?=null

ย  ย  ย  ย ย var resultinsec:TextView?=null

ย  ย  ย  ย ย var calculate:AppCompatButton?=null

ย  ย ย @SuppressLint(โ€œMissingInflatedIdโ€)

ย  ย ย @RequiresApi(Build.VERSION_CODES.N)

ย  ย ย override fun onCreate(savedInstanceState: Bundle?) {

ย  ย  ย  ย ย super.onCreate(savedInstanceState)

ย  ย  ย  ย ย setContentView(R.layout.activity_main)

ย  ย  ย  ย ย calendar=findViewById(R.id.calender)

ย  ย  ย  ย ย calendar2=findViewById(R.id.calender2)

ย  ย  ย  ย ย todaysdate=findViewById(R.id.distodaydate)

ย  ย  ย  ย ย dob=findViewById(R.id.disdateofbirth)

ย  ย  ย  ย ย result=findViewById(R.id.resultday)

ย  ย  ย  ย ย resulthours=findViewById(R.id.resultinhours)

ย  ย  ย  ย ย resultinmin=findViewById(R.id.resultinmin)

ย  ย  ย  ย ย resultinsec=findViewById(R.id.resultinsec)

ย  ย  ย  ย ย calculate=findViewById(R.id.calculatebtn)

ย  ย  ย  ย ย calendar!!.setOnClickListener({view->

ย  ย  ย  ย ย ClickDatePicker(view,todaysdate)

ย  ย  ย  ย })

ย  ย  ย  ย  ย calendar2!!.setOnClickListener({view->

ย  ย  ย  ย  ย ClickDatePicker(view,dob)

ย  ย  ย  ย })

ย  ย  ย  ย  ย calculate!!.setOnClickListener({view->

ย  ย  ย  ย  ย CalCulateAge(todaysdate,dob)

ย ย })

ย ย }

ย  ย ย private fun CalCulateAge(todaysdate: TextView?, dob: TextView?) {

ย  ย  ย  ย ย val TDate = todaysdate!!.getText().toString()

ย  ย  ย  ย ย val BDate = dob!!.getText().toString()

ย  ย  ย  ย ย val sdf =SimpleDateFormat(โ€œdd/MM/yyyyโ€,Locale.getDefault())

ย  ย ย try{

ย  ย  ย  ย  ย val d1 = sdf.parse(TDate)

ย  ย  ย  ย  ย val d2 = sdf.parse(BDate)

ย  ย  ย  ย  ย val todaysdateindays = d1.time/86400000

ย  ย  ย  ย  ย val dobdateindays = d2.time/86400000

ย  ย  ย  ย  ย val todaysdateinhours = d1.time/3600000

ย  ย  ย  ย  ย val dobdateinhours = d2.time/3600000

ย  ย  ย  ย  ย val todaysdateinmin = d1.time/60000

ย  ย  ย  ย  ย val dobdateinmin = d2.time/60000

ย  ย  ย  ย  ย val todaysdateinsec = d1.time/1000

ย  ย  ย  ย  ย val dobdateinsec = d2.time/1000

ย  ย  ย if(d2>d1)

ย  ย  ย  ย {

ย  ย  ย  ย  ย result!!.setText(โ€œDate of Birth cannot exceed the current Dateโ€)

ย  ย  ย  ย  ย resulthours!!.setText(โ€œDate of Birth cannot exceed the current Dateโ€)

ย  ย  ย  ย  ย resultinmin!!.setText(โ€œDate of Birth cannot exceed the current Dateโ€)

ย  ย  ย  ย  ย resultinsec!!.setText(โ€œDate of Birth cannot exceed the current Dateโ€)

ย  ย  ย  ย  ย  ย }

ย  ย  ย else{

ย  ย  ย  ย  ย val differenceday = todaysdateindays-dobdateindays

ย  ย  ย  ย  ย result!!.setText(โ€œAge in Days : โ€œ+differenceday+ โ€ daysโ€)

ย  ย  ย  ย  ย val differencehours = todaysdateinhours-dobdateinhours

ย  ย  ย  ย  ย resulthours!!.setText(โ€œAge in Hours : โ€œ+differencehours+ โ€ hoursโ€)

ย  ย  ย  ย  ย val differencemin = todaysdateinmin-dobdateinmin

ย  ย  ย  ย  ย resultinmin!!.setText(โ€œAge in Days : โ€œ+differencemin+ โ€ daysโ€)

ย  ย  ย  ย  ย val differencesec = todaysdateinsec-dobdateinsec

ย  ย  ย  ย  ย resultinsec!!.setText(โ€œAge in Days : โ€œ+differencesec+ โ€ daysโ€)

ย  ย  ย  ย  ย  ย }

ย  ย  ย  ย  ย  ย }catch(e:Exception)

ย  ย  ย  ย {

ย  ย  ย  ย  ย ย e.stackTrace

ย  ย  ย  ย  ย  ย }

ย  ย  ย  ย  ย  ย }

ย  ย  ย @RequiresApi(Build.VERSION_CODES.N)

ย  ย  ย private fun ClickDatePicker(view: View?, textview: TextView?) {

ย  ย  ย  ย  ย  ย val mycalender = Calendar.getInstance()

ย  ย  ย  ย  ย  ย val year = mycalender.get(Calendar.YEAR)

ย  ย  ย  ย  ย  ย val month = mycalender.get(Calendar.MONTH)

ย  ย  ย  ย  ย  ย val day = mycalender.get(Calendar.DAY_OF_MONTH)

ย  ย  ย  ย  ย  ย DatePickerDialog(this,DatePickerDialog.OnDateSetListener

ย  ย  ย  ย  ย  ย { veiw, year, month, day ->

ย  ย  ย  ย  ย  ย val selecteddate= โ€œ$day/${month+1}/$yearโ€

ย  ย  ย  ย  ย  ย textview!!.setText(selecteddate)ย 

ย  ย  ย  ย  ย  ย },year,month,day).show()

ย  ย  ย //Enjoy

ย  ย  ย  ย  ย  ย }

ย  ย  ย  ย  ย  ย }

  • Run your app on an emulator or a physical device to test it, and make any necessary changes to improve its functionality and user experience.

Complete Video of Age Calculator App


Share post on
Hammad
By Hammad

I'm Hammad Hasan your friendly tech enthusiast. I love sharing tips and tricks on all things tech through my blog, YouTube, and freelance work here at MyTechnologyHub.



Latest Posts

FFXIV Error Code I2501: Causes, Fixes, and Top Prevention Tips How To

FFXIV Error Code I2501: Causes, Fixes, and Top Prevention Tips

Final Fantasyย XIV, also calledย FFXIV, is an online role-playing game that allows players...

By Hammad
โ€ข
How to Fix Roblox Error Code 280 How To

How to Fix Roblox Error Code 280

Robloxย is a gaming platform on the internet where you can create your...

By Hammad
โ€ข
Canva Pro Team Invite Link Free Lifetime December 2023 How To

Canva Pro Team Invite Link Free Lifetime December 2023

Everyone wants to Access the premium Features of Canva free of cost....

By Hammad
โ€ข
How to Fix Error Code 403 in Roblox How To

How to Fix Error Code 403 in Roblox

Roblox has become this awesome online gaming platform where players can create...

By Hammad
โ€ข
How to Get Canva Pro for Free in 2023 How To

How to Get Canva Pro for Free in 2023

Canva is a design tool that has gained popularity worldwide, with over...

By Hammad
โ€ข
A Comprehensive Guide on Blocking Mind-Reading Technology Tech

A Comprehensive Guide on Blocking Mind-Reading Technology

Knowing how to protect your mind's privacy is more important than ever...

By Hammad
โ€ข
How to block numbers android How To

How to block numbers android

Comprehensive Guide to Blocking Numbers on Android In this modern world With...

By Hammad
โ€ข
Unveiling the Mystery: What is com.samsung.android.messaging? Tech

Unveiling the Mystery: What is com.samsung.android.messaging?

Samsung's default messaging app, called Com. Samsung.android.messaging or Samsung Messages, is very...

By Hammad
โ€ข