Author Topic: Make a age calculator for android using java  (Read 3966 times)

Musfiqur Rahman

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • Musfiqur Rahman
Make a age calculator for android using java
« on: March 21, 2023, 01:45:27 PM »
1.Create a new Android Studio project and add a layout file called "activity_main.xml".

2.Add a TextView to the layout to display the calculated age:


<TextView
    android:id="@+id/result_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your age is:"
    android:textSize="24sp"
    android:textStyle="bold"
    android:layout_marginTop="32dp"
    android:layout_centerHorizontal="true"/>
3. Add a DatePicker widget to allow the user to select their birthdate:

<DatePicker
    android:id="@+id/date_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:calendarViewShown="false"
    android:datePickerMode="spinner"
    android:layout_below="@+id/result_text_view"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"/>

4.In the MainActivity class, find the TextView and DatePicker widgets:


public class MainActivity extends AppCompatActivity {

    private TextView resultTextView;
    private DatePicker datePicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        resultTextView = findViewById(R.id.result_text_view);
        datePicker = findViewById(R.id.date_picker);
    }

    // ...
}

5.Add a button to the layout and define an onClickListener to calculate the age:

<Button
    android:id="@+id/calculate_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate"
    android:layout_below="@+id/date_picker"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"/>


public class MainActivity extends AppCompatActivity {

    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...

        Button calculateButton = findViewById(R.id.calculate_button);
        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int day = datePicker.getDayOfMonth();
                int month = datePicker.getMonth();
                int year = datePicker.getYear();

                Calendar birthdate = Calendar.getInstance();
                birthdate.set(year, month, day);

                Calendar today = Calendar.getInstance();
                int age = today.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
                if (today.get(Calendar.DAY_OF_YEAR) < birthdate.get(Calendar.DAY_OF_YEAR)) {
                    age--;
                }

                resultTextView.setText("Your age is: " + age);
            }
        });
    }

    // ...
}

This code will get the user's birthdate from the DatePicker widget, calculate their age based on the current date, and display the result in the TextView. Note that this code assumes that the user has already had their birthday this year. If you want to handle cases where the user hasn't had their birthday yet this year, you can adjust the calculation accordingly.