Create a new project in Android Studio and name it “AgeCalculatorApp”.
Open the activity_main.xml file located in the res/layout directory. Replace the existing code with the following XML layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/label_birthdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter your birthdate:" android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="16dp" /> <EditText android:id="@+id/edit_birthdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="date" android:layout_marginBottom="16dp" /> <Button android:id="@+id/button_calculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calculate" android:onClick="calculateAge" android:layout_marginBottom="16dp" /> <TextView android:id="@+id/text_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="16dp" /> </LinearLayout> |
Open the MainActivity.java file located in the java/<your_package_name> directory. Replace the existing code with the following Java code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.example.agecalculatorapp; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class MainActivity extends AppCompatActivity { private EditText editBirthdate; private TextView textResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editBirthdate = findViewById(R.id.edit_birthdate); textResult = findViewById(R.id.text_result); } public void calculateAge(View view) { String birthdateString = editBirthdate.getText().toString(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()); Date currentDate = new Date(); try { Date birthdate = dateFormat.parse(birthdateString); long ageInMillis = currentDate.getTime() - birthdate.getTime(); // Calculate age in years long ageInYears = ageInMillis / (1000L * 60L * 60L * 24L * 365L); // Calculate age in months long ageInMonths = ageInYears * 12; // Calculate age in days long ageInDays = ageInMillis / (1000L * 60L * 60L * 24L); String result = String.format("Age: %d years, %d months, %d days", ageInYears, ageInMonths, ageInDays); textResult.setText(result); } catch (ParseException e) { e.printStackTrace(); textResult.setText("Invalid date format"); } } } |
Open the AndroidManifest.xml file located in the app/src/main directory. Replace the existing code with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.agecalculatorapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Open the build.gradle (Module: app) file located in the root directory of the project. Ensure that the minSdkVersion is set to 21 or higher:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
apply plugin: 'com.android.application' android { compileSdkVersion 30 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.example.agecalculatorapp" minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } lintOptions { checkReleaseBuilds false abortOnError false } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } |
That’s it! You can now build and run the app on an Android device or emulator. The app will allow the user to enter their birthdate and calculate their age in years, months, and days.