1

In my app i have dailog with edittext and submit button, if the edittext is empty i gave a toast message "please enter something" ..my code is below

Button reviewPost = (Button) dialog.findViewById(R.id.submit);
        reviewPost.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.setEnabled(false);
                    EditText ratingComment = (EditText) dialog
                            .findViewById(R.id.reviewcomment);
                    String comment = ratingComment.getText().toString();



                    String postStatus = "Y";
                    if (spinner.getSelectedItemPosition() > 0) {
                        postStatus = "N";
                    }
                    RadioGroup rating = (RadioGroup) dialog
                            .findViewById(R.id.customrating);
                    int radioButtonID = rating.getCheckedRadioButtonId();
                    View radioButton = rating.findViewById(radioButtonID);
                    int ratingNo = rating.indexOfChild(radioButton);

                    String ratingValue = (ratingNo + 1) + "";

                    if (comment != null && !comment.equalsIgnoreCase("")){

                    new PostReviewMessage().execute(activity, comment,
                            ratingValue, postStatus); 
                    activity.finish();
                    }else{
                         Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();

                     }  
                    Constants.rivewDialog = new ProgressDialog(activity);
                    Constants.rivewDialog.setCanceledOnTouchOutside(false);
                    Constants.rivewDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    Constants.rivewDialog.setMessage(activity.getString(R.string.postingrivew));
                    Constants.rivewDialog.setOnCancelListener(new OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface arg0) {

                        }
                    });
                    Constants.rivewDialog.show();
                    dialog.dismiss();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                }
                return false;
            }
            }); 

But even if the edittext is empty it is submitting which should not happen.i am checking the condition as follows but its not working

if (comment != null && !comment.equalsIgnoreCase("")){

                    new PostReviewMessage().execute(activity, comment,
                            ratingValue, postStatus); 
                    activity.finish();
                    }else{
                         Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();

                     }
hari86
  • 659
  • 2
  • 16
  • 28

4 Answers4

0

use comment.trim().equals("") instead of comment.equalsIgnoreCase("")

or put a check on comment string length that it should exceed the minimum characters.

Ameer Moaaviah
  • 1,530
  • 12
  • 27
0

use

if(comment != null && !comment.isEmpty())
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

You can used this.

if(comment != null && !comment.isEmpty() && comment.length()!=0){
    //your task
}else{
  //give some message
}
Harshid
  • 5,701
  • 4
  • 37
  • 50
0

Solved with below code..the problem is with the v.setEnabled(false);...i moved it into the if condition

if (comment != null && !comment.equalsIgnoreCase("")){
                            v.setEnabled(false);
                            new PostReviewMessage().execute(activity, comment,
                                    ratingValue, postStatus); 
                            Constants.rivewDialog = new ProgressDialog(activity);
                            Constants.rivewDialog.setCanceledOnTouchOutside(false);
                            Constants.rivewDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                            Constants.rivewDialog.setMessage(activity.getString(R.string.postingrivew));


                            Constants.rivewDialog.setOnCancelListener(new OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface arg0) {

                                }
                            });
                            Constants.rivewDialog.show();
                            dialog.dismiss();
                         }else{

                             Toast.makeText(activity, "Please enter your comment", Toast.LENGTH_SHORT).show();
                         }  
hari86
  • 659
  • 2
  • 16
  • 28