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:
- Open Android Studio and create a new project usingย File> New > New Project.
- ย Select โEmpty Activityโ as the template for your project, and give your project a name and a package name.
- ย 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.