1

I'm trying to validate an TextInputLayout in an AlertDialog.Builder. Pls help me. I've tried to validate it in a simple way but when the positive button is pressed, nothing happens.

Thanks

    private TextInputLayout txtUpdateWeight;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme);

    final FirebaseDatabase db = FirebaseDatabase.getInstance();

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.layout_update_weight_dialog, null);

    txtUpdateWeight = view.findViewById(R.id.tlUpdateWeight);

    builder.setView(view)
            .setTitle("Update Weight")
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setPositiveButton("Send", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (!validateField()) {
                        return;
                    }
                    String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();

                    DatabaseReference myDB = db.getReference().child("Additional details");
                    Map<String, Object> updates = new HashMap<>();

                    updates.put("weight", etWeight);

                    myDB.updateChildren(updates);

                    Toast.makeText(getContext(), "Weight updated Successfully", Toast.LENGTH_SHORT).show();
                }
            });

    return builder.create();
}

This is my validation function

public boolean validateField() {
    String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();

    if (etWeight.isEmpty()) {
        txtUpdateWeight.setError("Enter your weight");
        return false;
    } else {
        txtUpdateWeight.setError(null);
        return true;
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
OoMaR jOhUr
  • 1,503
  • 2
  • 7
  • 10

2 Answers2

4

You should use TextUtils

  if(TextUtils.isEmpty(etWeight))

Note

You should use TextInputEditText instead of TextInputLayout.

I assume your XML is

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >

            <android.support.design.widget.TextInputEditText
                android:id="@+id/your_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </android.support.design.widget.TextInputLayout>

Then

 String etWeight = textInputEditTextOBJ.getText().toString().trim();
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

Try this

public boolean validateField() {
String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();

if (TextUtils.isEmpty(etWeight)) {
    txtUpdateWeight.setError("Enter your weight");
    return false;
} else {
    txtUpdateWeight.setError(null);
    return true;
}}

Add else also

   if (!validateField()) {
                        return;
                    }else{
                    String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();

                    DatabaseReference myDB = db.getReference().child("Additional details");
                    Map<String, Object> updates = new HashMap<>();

                    updates.put("weight", etWeight);

                    myDB.updateChildren(updates);

                    Toast.makeText(getContext(), "Weight updated Successfully", Toast.LENGTH_SHORT).show();
}
mayank modi
  • 129
  • 1
  • 4
  • No, it does not validate the field and closes instantly – OoMaR jOhUr Oct 05 '18 at 08:11
  • Hello, after a deep research i've found an answer at that link. https://stackoverflow.com/questions/11363209/alertdialog-with-positive-button-and-validating-custom-edittext/11363525 Thanks to help – OoMaR jOhUr Oct 05 '18 at 19:05