1

I wanna disable positive button when one of input fields is empty. I tried to use ((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); But it does not work.

new AlertDialog.Builder(MainActivity.this)
                    .setView(viewInput)
                    .setTitle("Add task")
                    .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            String title = edtTitle.getText().toString();
                            String des = edtDes.getText().toString();
                            String date = edtDate.getText().toString();
                            String time = edtTime.getText().toString();

                            if(isEmpty(title) || isEmpty(des) || isEmpty(date) || isEmpty(time)){
                                ((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                                Toast.makeText(MainActivity.this,"Please, enter all fields.",Toast.LENGTH_SHORT).show();
                            }else{
                                Task task = new Task(title, des, date, time);

                                boolean isInserted = new TaskHandler(MainActivity.this).create(task);

                                if(isInserted){
                                    Toast.makeText(MainActivity.this,"Task Saved",Toast.LENGTH_SHORT).show();
                                    loadTasks();
                                }else{
                                    Toast.makeText(MainActivity.this,"Unable to save",Toast.LENGTH_SHORT).show();

                                }
                                dialogInterface.cancel();
                            }
                        }
                    }).show();

1 Answers1

0

You disable positive button after DialogInterface.OnClickListener. it means your positive button already set on your AlertDialog... according to your need you have to disable positive button before you show dialog...

check this link...

Reference

Jay Panchal
  • 257
  • 1
  • 7