Android input validation when a button is pressed and display the validation message in a dialog as shown below.
Codename is required
Password is required
USername is required.
Enter the valid Email id.
Dismiss
This is my code:
btninsert = (Button)findViewById(R.id.new_customer);
btninsert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Firstname = (EditText) findViewById(R.id.tf_userName);
EditText Lastname = (EditText) findViewById(R.id.tf_password);
EditText Baddress = (EditText) findViewById(R.id.tf_address);
if(Firstname.getText().toString().length() == 0 )
Firstname.setError( "Firstname is required!" );
else if(Lastname.getText().toString().length() == 0 )
Lastname.setError( "Lastname is required!" );
else if(Baddress.getText().toString().length() == 0 )
Baddress.setError( "Address is required!" );
else
insertValues();
}
});
How can i validate.
Change current code as for input validation on EditText show validation message in a dialog :
First create a method for showing Alert as:
Make a method to check validation on String :
call this method on button click as:
We can do the edittext validation inside a dialog box like below. Decide the scope according to your requirement.