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