Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to check if all EditText are empty

Tags:

android

I have 6 EditText fields in an xml.. on Button click I need to validate whether all EditText have values or it is empty. Currently I am check each EditText one by one.. How can I check all at once.

the Code

private Button BtnSave;
 EditText ev_last_name,ev_first_name,ev_email,ev_password,ev_confirm_password,ev_phone;
 String last_name,first_name,email,password,confirm_password,phone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
    BtnSave=(Button) findViewById(R.id.BtnSave);
    ev_last_name=(EditText)findViewById(R.id.edit_lname);
    ev_first_name=(EditText)findViewById(R.id.edit_fname);
    ev_email=(EditText)findViewById(R.id.edit_email);
    ev_password=(EditText)findViewById(R.id.edit_passwd);
    ev_confirm_password=(EditText)findViewById(R.id.edit_cpasswd);
    ev_phone=(EditText)findViewById(R.id.edit_phone);



    BtnSave.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             last_name=ev_last_name.getText().toString();
             first_name=ev_first_name.getText().toString();
             email=ev_email.getText().toString();
             password=ev_password.getText().toString();
             confirm_password=ev_confirm_password.getText().toString();
             phone=ev_phone.getText().toString();

             if ((ev_last_name.getText().toString().length() <= 0))
             {
                 System.out.println(" The EditText is empty");
                 //I will use the toast later
             }


        }
    });


}
like image 606
Srikanth Pai Avatar asked Jul 24 '12 14:07

Srikanth Pai


People also ask

How do I know if EditText is empty Kotlin?

This example demonstrates how to Check if Android EditText is empty in Kotlin. Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

What does an empty EditText return?

It will return true if its empty/null.

How can I tell if EditText is clicked?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.


1 Answers

Use a "for" cycle.

private boolean validate(EditText[] fields){
    for(int i = 0; i < fields.length; i++){
        EditText currentField = fields[i];
        if(currentField.getText().toString().length() <= 0){
            return false;
        }
    }
    return true;
}

and use the method like this:

boolean fieldsOK = validate(new EditText[] { ev_last_name, ev_first_name, ev_email })

will return true if all fields are non empty.

like image 200
Andy Res Avatar answered Oct 11 '22 20:10

Andy Res