Date of Birth Calculator in |Sketchware

Date of Birth Calculator in |Sketchware


To create an Age Calculator App using DatePickerDialog in Sketchware follow the steps given below.

1. In VIEW area of your sketchware android project, insert three TextViews textview_dobtextview_age_daystextview_age, and an ImageView imageview1.


2. Create a more block extra.

3. In the more block extra use add source directly block and put following code.

}

public static class DatePickerFragment extends androidx.appcompat.app.AppCompatDialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
int mon = month +1;
Calendar now = Calendar.getInstance();
Calendar birthDay = Calendar.getInstance();

TextView textview_dob = getActivity().findViewById(R.id.textview_dob);
TextView textview_age_days = getActivity().findViewById(R.id.textview_age_days);
TextView textview_age = getActivity().findViewById(R.id.textview_age);

String date = day + "/" + mon + "/" + year;
textview_dob.setText(date);

birthDay.set(Calendar.YEAR, year);
birthDay.set(Calendar.MONTH, month);
birthDay.set(Calendar.DAY_OF_MONTH, day);

double diff = (long)(now.getTimeInMillis() - birthDay.getTimeInMillis());

if (diff < 0) {
textview_dob.setText("Selected date is in future.");
textview_age.setText("");
} else {
double days = diff/(1000*60*60*24);
textview_age_days.setText((int)days + " days");
int age_years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);

int currMonth = now.get(Calendar.MONTH) + 1;
int birthMonth = birthDay.get(Calendar.MONTH) + 1;
int age_months = currMonth - birthMonth;
if (age_months < 0){
age_years--;
age_months = 12 - birthMonth + currMonth;
if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
age_months--;
}
} else if (age_months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
age_years--;
age_months = 11;
}
int age_days = 0;
if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE)){
age_days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
} else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
int today = now.get(Calendar.DAY_OF_MONTH);
age_months--;
age_days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
} else {
age_days = 0;
if (age_months == 12){
age_years++;
age_months = 0;
}
}
textview_age.setText(age_years + " years, " + age_months + " months, " + age_days + " days");
}
}

4. Create a More Block showDatePicker. In this block, use an add source directly block and put following code.
androidx.appcompat.app.AppCompatDialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "Date Picker");

5. In imageview1 onClick event use More Block showDatePicker.

6. Save and run the project. Now on clicking the ImageView DatePickerDialog will appear from which user can select date of birth. The date selected is displayed in textview_dob, and the age is displayed in textview_age_days and textview_age.