Age Calculator App 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 a LinearH and inside it insert an EditText edittext1 for displaying Date of Birth, and an ImageView imageview1, for displaying calendar symbol.
Insert another LinearH and inside it insert an EditText edittext2, for displaying the age.

2. Create a more block extra.

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

// Define showDatePickerDialog(View).
public void showDatePickerDialog(View v) {
// Create and show a new DatePickerFragment.
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}

// Define a DialogFragment class DatePickerFragment.
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
// Define a new Calendar for present date
Calendar now = Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Create DatePickerFragment (a DialogFragment) with a new DatePickerDialog,
// Present day of month, month, and year are set as the day, month, and year of this DatePickerDialog.
int y = now.get(Calendar.YEAR);
int m = now.get(Calendar.MONTH);
int d = now.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, y, m, d);
}

// When Date if birth is selected
public void onDateSet(DatePicker view, int year, int month, int day) {
int mon = month +1;
// Define a new Calendar for birth date.
Calendar birthDay = Calendar.getInstance();
String date = day + "/" + mon + "/" + year;
// Define the two EditText again using their IDs in main.xml.
EditText edittext21 = getActivity().findViewById(R.id.edittext1);
EditText edittext22 = getActivity().findViewById(R.id.edittext2);
edittext21.setText(date);
// Set the selected year, month, and day as the year, month, and day of Calendar birthDay.
birthDay.set(Calendar.YEAR, year);
birthDay.set(Calendar.MONTH, month);
birthDay.set(Calendar.DAY_OF_MONTH, day);
// find difference between present date and selected date in milliseconds.
double diff = (long)(now.getTimeInMillis() - birthDay.getTimeInMillis());
// If difference is less than 0, show message that selected date is in future.
if (diff < 0) {
Toast.makeText(getContext(), "Selected date is in future.", Toast.LENGTH_SHORT).show();
edittext22.setText("");
} else {
// Get difference between years
int years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
int currMonth = now.get(Calendar.MONTH) + 1;
int birthMonth = birthDay.get(Calendar.MONTH) + 1;
// Get difference between months
int months = currMonth - birthMonth;
// If month difference is negative then reduce years by one
// and calculate the number of months.
if (months < 0){
years--;
months = 12 - birthMonth + currMonth;
if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
months--;
} else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)){
years--;
months = 11;
}
// Calculate the days
int days = 0;
if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
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);
now.add(Calendar.MONTH, -1);
days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
} else {
days = 0;
if (months == 12){
years++;
months = 0;
}
}
// Display the age in years, months and days
edittext22.setText(years + " years, " + months + " months, " + days + " days");
}
}

4. In imageview1 onClick event use an add source directly block and put following code.
showDatePickerDialog(imageview1);

5. 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 edittext1, and the age is displayed in edittext2.

Comments

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Simple car racing android game in Sketchware

Firebase auth in Sketchware for Login Activity