I am writing a age calculator feature where user enters the value for its age in years(birth year) and in months(birth of month) and based on the entered values it calculates the age.
There are two edittext
one for (age in year) and other for (age in months) but I am not able to get those values calculated to get the users date of birth.
so just be to clear here is what i want enter image description here so when user enter age 6 and months 3 so it should display 2011-9-01
Note : I am using textwatcher
here.
Here is the code for age in years same logic goes for age in months.
mAgeInYears.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().trim().isEmpty()) {
if (getCurrentFocus().getId() == mAge.getId() || mDOB.getText().toString().trim().isEmpty()) {
Calendar calendar = Calendar.getInstance();
int curYear = calendar.get(Calendar.YEAR);
int curMonth = calendar.get(Calendar.MONTH);
int birthYear = curYear - Integer.valueOf(s.toString().trim());
String calcDOB = String.valueOf(birthYear) + "-" + (String.valueOf(curMonth))+ "-01";
mDOB.setText(calcDOB);
}
}