0

I am developing a application in which if i click a button it woul d check that all the edit text are empty or something written and providing similar alert box for that message.I had made a method and calling it in Onclick event.But i am not able to get the desired result as syntax error is not dr even logcat not showing any error please help me out...Thanks in advance..

    ImageButton b2=(ImageButton)findViewById(R.id.imageButton2);

   b2.setOnClickListener(new OnClickListener()
   {

    @Override
    public void onClick(View arg0) {
        checkValue();

    }

    private void checkValue() {
        EditText e = (EditText) findViewById(R.id.editText1);
           EditText e1 = (EditText) findViewById(R.id.editText2);
           EditText e2 = (EditText) findViewById(R.id.editText3);
           EditText e3 = (EditText) findViewById(R.id.editText4);
           EditText e4 = (EditText) findViewById(R.id.editText5);
           EditText e5 = (EditText) findViewById(R.id.editText6);
        String f = e.getText().toString();
         String f1 = e1.getText().toString(); 
           String f2 = e2.getText().toString();
           String f3 = e3.getText().toString();
           String f4 = e4.getText().toString();
           String f5 = e5.getText().toString();

        if ((f.length()>0)&&(f1.length()>0)&&(f2.length()>0)&&(f3.length()>0)&&(f4.length()>0)&&(f5.length()>0)) { 

            AlertDialog alert= new AlertDialog.Builder(Test.this).create();
            alert.setTitle("Exception:Incomplete Form");
            alert.setMessage("Looks like you missed a field or made a mistake.Please ensure all fields of form are filled.Click OK to go to main page");

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int which) {
                      Intent i=new Intent(Test.this,ShpoonkleActivity.class);

                } }); 

           } 
        else
        {
            AlertDialog alert= new AlertDialog.Builder(Test.this).create();
            alert.setTitle("Your Form is successfully processed.Thanks");
            alert.setMessage("You are Done! Keep an eye out for a confirmation email. If you don't get one in the next 24h, open this form again, and make sure youremail is correct");

        }


    }
    });

}
    }
Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
Gaurav Chawla
  • 1,847
  • 3
  • 18
  • 26
  • Can you describe what the current result is, if it's not the desired one. Are you saying, you don't get any errors, but the alert dialog with the 'Exception: Incomplet form' just never shows up, but only the 'You are done' dialog? – Mathias Conradt Nov 01 '11 at 10:59
  • if ((f.length()>0)&&(f1.length()>0)&&(f2.length()>0)&&(f3.length()>0)&&(f4.length()>0)&&(f5.length()>0)) .... Here you're saying that all the fields are not empty, but in your dialog you're saying you missed a field :P – Carnal Nov 01 '11 at 11:02

3 Answers3

6

// Edit

I think you have it the wrong way, where your if statment is when every is filled in, your else when one of the fields is empty

---- previous answer ----

You can do it like this.

if(editText.getText().toString().length() > 0) {
     // editText is not empty
} 

if(editText.getText().toString().length() == 0) {
     // editText is empty
}
Mats Hofman
  • 7,060
  • 6
  • 33
  • 48
6

you can try this :

if ((f.trim().equals('') || (f1.trim().equals('') ||........) { 

    // Error , one or more editText are empty

}
else
{
    // all editText are not empty 
}
Houcine
  • 24,001
  • 13
  • 56
  • 83
6

Mats and Houcine answers are valid but you can even do shorter using isEmpty():

if(editText.getText().toString().isEmpty()) {
     // editText is empty
} else {
     // editText is not empty
}
Romain Piel
  • 11,017
  • 15
  • 71
  • 106
  • Thanks to all for responding further my answer to the problem i asked is actually my logic and syntax was all was fine but i only missed a simple decleration to write that alertBoxObject.show(); thats why no alert box was not showing ... – Gaurav Chawla Nov 01 '11 at 11:53