How to make age Calculator app in Android Studio
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 … Read more