0

I have put a boolean condition inside a Alertdialog "OK" button..but that condition doesn't seem to be working..on top of that..even after commenting dialog.dismiss()..the alert dialog still gets dismissed when i used for some checking..

this is the alert dialog small code..

ab.setCancelable(false).setPositiveButton("Add", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {

       if(ValidationsForNewCelebration()){
           str1=et1.getText().toString();
           str2=et2.getText().toString();
           //  AddNewTask anct=new AddNewTask();
           //  anct.execute();
           //  dialog.dismiss();
       } else {

       }     
   }
}

and this my boolean conditional code..

protected boolean ValidationsForBlankFields(){
    boolean allFieldsValid=true;
    if(et1.getText().toString().length()<=0){
        Toast.makeText(StartingActivity.this, "Field should not be kept Blank ", Toast.LENGTH_SHORT).show();         
        //return false;
        allFieldsValid=false;
    }
    else if(et2.getText().toString().length()<=0){
         Toast.makeText(StartingActivity.this, "Field should not be kept Blank ", Toast.LENGTH_SHORT).show();
        //return false;
        allFieldsValid=false;
    }
    else if(tv1.getText().toString().length()<=0){
         Toast.makeText(StartingActivity.this, "Please fill up the Blank Field", Toast.LENGTH_SHORT).show();
         allFieldsValid=false;
    }
    else if(tv2.getText().toString().length()<=0){
         Toast.makeText(StartingActivity.this, "Please fill up the Blank Field", Toast.LENGTH_SHORT).show();
         allFieldsValid=false;
    }
    if(!allFieldsValid){
          return false;
    } else {
          return true;
    }
} 

tv1,*tv2* are two textviews while et1 and et2 are edittext. Condition is to check whether they are empty or not.

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
AndroidMech
  • 1,676
  • 3
  • 20
  • 31

1 Answers1

2

Use this condition for all, trim()

 if(editext.getText().toString().trim().length()!=0)

in ValidationsForBlankFields method, return allFieldsValid; No need to put if else condition for that...

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Thanks a lot..will try..and any idea why alertdialog gets dismissed even when dialog.dismiss() is commented out?? – AndroidMech Mar 31 '14 at 07:01
  • @MarcoAcierno Even I thought that was my mistake..on when even when i click on the OK button of the AlertDialog..the dialog gets dismissed.. – AndroidMech Mar 31 '14 at 07:05
  • 1
    http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked – Samir Mangroliya Mar 31 '14 at 07:06