-2

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);

                }
            }
Aniket
  • 982
  • 1
  • 11
  • 16

1 Answers1

0

You can try this way, Below is the code snippet to resolve your problem.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText edtYear;
    private EditText edtMonth;
    private EditText edtDob;

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

        edtYear = (EditText) findViewById(R.id.edtYear);
        edtMonth = (EditText) findViewById(R.id.edtMonth);
        edtDob = (EditText) findViewById(R.id.edtDob);

        edtYear.addTextChangedListener(textWatcher);
        edtMonth.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = 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) {
            setDobFromYearAndMonth();
        }
    };

    private void setDobFromYearAndMonth() {
        int year = 0;
        int month = 0;
        if (!TextUtils.isEmpty(edtYear.getText())) {
            year = Integer.parseInt(edtYear.getText().toString());
        }
        if (!TextUtils.isEmpty(edtMonth.getText())) {
            month = Integer.parseInt(edtMonth.getText().toString());
        }

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -year);
        calendar.add(Calendar.MONTH, -month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        edtDob.setText(sdf.format(calendar.getTimeInMillis()));
    }
}

Output:

I hope it will help you.

Piyush Malaviya
  • 1,091
  • 13
  • 17
  • thnx @Piyush Malaviya can u just take a look again over the edited question – Aniket Dec 17 '17 at 07:38
  • thnx a lot @Piyush Malaviya it just worked as a charm but cant upvote your answer coz m new to stackoverflow any ways thnx a lot again . – Aniket Dec 18 '17 at 06:52