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

How to Get Grammarly Premium Cookies 2024 Tools

How to Get Grammarly Premium Cookies 2024

Grammarly is a widely popular online writing tool that helps users improve...

By Hammad
Unlocking the Power of LongTail Pro Premium Cookies 2024 Tools

Unlocking the Power of LongTail Pro Premium Cookies 2024

Search Engine Optimization (SEO) is not just a buzzword; it's a critical...

By Hammad
How to Get QuillBot Premium Cookies 2024 Tools

How to Get QuillBot Premium Cookies 2024

QuillBot is an advanced writing assistant that uses artificial intelligence to help...

By Hammad
Unlock Your Writing Potential with Premium Wordtune Cookies Tools

Unlock Your Writing Potential with Premium Wordtune Cookies

Have you ever struggled to find the right words, the perfect turn...

By Hammad
Get Semrush Premium Cookies for Free 2024 Tools

Get Semrush Premium Cookies for Free 2024

In the fast-paced world of digital marketing, having the power of analytics...

By Hammad
How to Get Udemy Premium Cookies in 2024[Daily Updated] How To

How to Get Udemy Premium Cookies in 2024[Daily Updated]

Want to learn new things but don't want to pay for expensive...

By Hammad
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