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 -
final EditText input1 = new EditText(MainActivity.this);
final EditText input2 = new EditText(MainActivity.this);
input1.setHint("Enter name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(input1);
linearLayout.addView(input2);
final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Sign In Failed")
.setCancelable(false)
.setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
builder.show();
((AlertDialog)builder).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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"