6

Here is my code for a dialog, I want to disable positive button if text size in edit-text in greater than 5 and enable it if size <= 5

private void myDialog(String title) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);


            // Get the layout inflater
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View dialogView = inflater.inflate(R.layout.new_dialog, null);

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(dialogView);

            final EditText dialogEdittext = (EditText) dialogView.findViewById(R.id.dialog_editText);
            final TextView dialogMessage = (TextView) dialogView.findViewById(R.id.dialog_limit);

            dialogEdittext.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {

                    // if text length is greater than 5 disable positive button
                                // else enable 
                }
            });

            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                }
            });

            final Dialog dialog = builder.create();
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            dialog.show();

        }
Androider
  • 2,884
  • 5
  • 28
  • 47
  • refer this link http://stackoverflow.com/questions/8238952/how-to-disable-enable-dialog-negative-possitive-buttons – Dixit Patel Jan 19 '13 at 11:49
  • i already have seen this link, it will disable/enable button only once when dialog is created, but i want this every time user enter a character in edittext – Androider Jan 19 '13 at 11:58
  • I never do by this way. i have did tihs using custom dialog with button and in that check the validation. you can do by that way. try it – Chintan Khetiya Jan 19 '13 at 12:32

1 Answers1

16

Try something like this:

@Override
public void afterTextChanged(Editable s) {
     if (s.length() > 5) {
        dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
     } else {
        dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
     }
}

where dialog is:

final AlertDialog dialog = builder.create();
user
  • 86,916
  • 18
  • 197
  • 190
  • 4
    worked but i had to shift dialogEdittext.addTextChangedListener..... below ....dialog.show(); – Androider Jan 19 '13 at 12:03
  • @SyedZahidAli That was required because you declared the `TextWatcher` before the `dialog` variable was created so it couldn't see it. – user Jan 19 '13 at 12:36