Home > Software design >  CustomDialog disable button "Ok" and active when all fields are valid
CustomDialog disable button "Ok" and active when all fields are valid

Time:08-16

The code shows a custom dialog in which information must be entered, in order to send and then give "Ok" all fields must be valid. For now I managed to do only when the last field is valid the "Ok" button is activated and then you can send the data. How do I make sure that the "Ok" button is activated only when all the fields are valid?

public void onClick(View view) {

            View viewcustom = getLayoutInflater().inflate(R.layout.customdialog, null);

            EditText edt1 = viewcustom.findViewById(R.id.description);
            EditText edt2 = viewcustom.findViewById(R.id.age);
            EditText edt3 = viewcustom.findViewById(R.id.hobby);
            EditText edt4 = viewcustom.findViewById(R.id.city);
            EditText edt5 = viewcustom.findViewById(R.id.address);
            
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireActivity())
                        .setView(viewcustom)
                        .setPositiveButton("Ok", (dialogInterface, i) -> {

                            String Description = edt1.getText().toString();
                            String Age = edt2.getText().toString();
                            String Hobby = edt3.getText().toString();
                            String City = edt4.getText().toString();
                            String address = edt5.getText().toString();


                            firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

                            reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
                            reference.child("description").setValue(Description);
                            reference.child("age").setValue(Age);
                            reference.child("hobby").setValue(Hobby);
                            reference.child("city").setValue(City);
                            reference.child("address").setValue(address);


                        });
                   alertDialog.setNegativeButton("Cancel", null);
                AlertDialog ad = alertDialog.show();
                Button ok = ad.getButton(AlertDialog.BUTTON_POSITIVE);
                ok.setEnabled(false);


            final TextWatcher _checker = new TextWatcher() {

                @Override public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) {}

                @Override public void onTextChanged(CharSequence sequence, int i, int i1, int i2) {}

                @Override
                public void afterTextChanged(Editable editable) {
                    boolean isValid = false;

                    if (edt1.getText().length() == 0 | edt2.getText().length() == 0 | edt3.getText().length() == 0 | edt4.getText().length() == 0 | edt5.getText().length() == 0) { isValid = true; }

                    ok.setEnabled(isValid);
                }
            };

            edt1.addTextChangedListener(_checker);
            edt2.addTextChangedListener(_checker);
            edt3.addTextChangedListener(_checker);
            edt4.addTextChangedListener(_checker);
            edt5.addTextChangedListener(_checker);


            // Initially OK button is disabled.
            ad.show();
            ad.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
        }
});

CodePudding user response:

You need to do like this:

    ...
    AlertDialog ad = alertDialog.show();
    final Button ok = ad.getButton(AlertDialog.BUTTON_POSITIVE);
    ok.setEnabled(false);

    final TextWatcher _checker = new TextWatcher() {

        @Override public void beforeTextChanged(CharSequence sequence, int i, int i1, int i2) {}

        @Override public void onTextChanged(CharSequence sequence, int i, int i1, int i2) {}

        @Override
        public void afterTextChanged(Editable editable) {
            boolean isValid = false;

            //... Check if valid here

            ok.setEnabled(isValid);
        }
    };

    edt1.addTextChangedListener(_checker);
    edt2.addTextChangedListener(_checker);
    edt3.addTextChangedListener(_checker);
    edt4.addTextChangedListener(_checker);
    edt5.addTextChangedListener(_checker);
    ...
  • Related