Search code examples
androidandroid-layout

Android ask for next input only if the prior input is yes


I am implementing a Data Request Form via native android, where the user is supposed to enter various fields. Consider below scenario :

  1. One of the fields asks Insurance?, and it seeks Yes/No input.
  2. If the input of the prior field was Yes, only then it should ask for Insurance Number .
Label Input
Insurance? (TextView) * Yes * No (RadioGroup)

If the prior input is Yes, only then we should get a next request like:

Label Input
Insurance Number (TextView) ......................... (EditView)

Can someone please help me understand how can I implement this? Tried searching for this query but couldn't find. If this question is duplicate, please let me know.


Solution

  • Here, when the user selects "Yes" in the "Insurance?" field, the "Insurance Number" field becomes visible, and when they select "No," it becomes hidden.res

         RadioGroup radioGroupInsurance =findViewById(R.id.radioGroupInsurance);
          TextView textViewInsuranceNumber = findViewById(R.id.textViewInsuranceNumber);
         EditText editTextInsuranceNumber =findViewById(R.id.editTextInsuranceNumber);
    
        radioGroupInsurance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == R.id.radioYes) {
                textViewInsuranceNumber.setVisibility(View.VISIBLE);
                editTextInsuranceNumber.setVisibility(View.VISIBLE);
            } else if (checkedId == R.id.radioNo) {
                textViewInsuranceNumber.setVisibility(View.GONE);
                editTextInsuranceNumber.setVisibility(View.GONE);
            }
        }
    });