Got a few problem here. I have created a arrays of EditText and it is working fine. Now I am getting an error when one of the EditText is empty.
Here is my code:
int[] textIDs = new int[] {R.id.etFirstName, R.id.etLastName, R.id.etEmail, R.id.etAddress, R.id.etCity, R.id.etRegion, R.id.etMobile, R.id.etLandLine, R.id.etYear, R.id.etMonth, R.id.etDay };
for(int j=0; j<textIDs.length; j++) {
EditText editText = (EditText) findViewById(textIDs[j]);
if(editText.getText().toString().trim().equals(""))
{
// editText is empty
Toast.makeText(MainActivity.this, "Request cannot performed..\n Please ensure all fields are filled", Toast.LENGTH_SHORT).show();
break;
}
else
{
// editText is not empty
Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
}
The main problem using this code is that the loop continues to do its function that this code
Toast.makeText(MainActivity.this, "12", Toast.LENGTH_SHORT).show();
continues to show in every loop. Is there any way that this Toast shows after the looping is done?
by using a boolean you will only show the toast for the else statement once, after the loop has exited, and only if the else statement was called, so only if “editText is not empty”