Skip to content Skip to sidebar Skip to footer

How To Validate Text Boxes In Alert Dialog Box In Android

i am working on alert dialog with some text boxes where the text boxes needed to be validated and if they are correct only then the pop up message need to be disappeared The proble

Solution 1:

You can either call your text validation method on positive click button with a condition with a toast message. or you can add addTextChangeListener to your textview where you are entering text, to call validation method everytime you change text.

Solution 2:

First import class as -import android.support.v7.app.AlertDialog;

Then Try this -

finalEditTextinput1=newEditText(MainActivity.this);
        finalEditTextinput2=newEditText(MainActivity.this);
        input1.setHint("Enter name1");
        input2.setHint("Enter Name2");
        LinearLayoutlinearLayout=newLinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(input1);
        linearLayout.addView(input2);

        finalAlertDialogbuilder=newAlertDialog.Builder(MainActivity.this)
                .setTitle("Sign In Failed")
                .setCancelable(false)
                .setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                    }
                }).create();
        builder.show();
        ((AlertDialog)builder).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if (input1.length() <= 0) {
                    Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    builder.dismiss();
                }
            }
        });

Post a Comment for "How To Validate Text Boxes In Alert Dialog Box In Android"