0

I only want my AlertDialog to be dismissed when certain conditions are met (when name and surname given are valid) - else it should always be on top of the parent view. My code is this:

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final TextView instructions = new TextView(this);
    instructions.setText(R.string.alert_enter_data);
    final EditText name = new EditText(this);
    name.setHint(R.string.name);
    final EditText surname = new EditText(this);
    surname.setHint(R.string.surname);
    LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.addView(instructions);
    ll.addView(name);
    ll.addView(surname);
    alert.setView(ll);
    alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String name_txt = name.getText().toString();
            String surname_txt = surname.getText().toString();
            if ((name_txt.length() > 1) && (surname_txt.length() > 1)) {
                dialog.dismiss();
            }
        }
    });
    final AlertDialog alert_dialog = alert.create();
    alert_dialog.setCanceledOnTouchOutside(false);
    alert_dialog.show();

With this code the AlertDialog disappears when the button is pressed, regardless of the input text. I then tried this:

alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String name_txt = name.getText().toString();
                String surname_txt = surname.getText().toString();
                String email_txt = email.getText().toString();
                String cellphone_txt = cellphone.getText().toString();
                String postcode_txt = postcode.getText().toString();
                if ((name_txt.length() > 1) && (surname_txt.length() > 1) && (email_txt.length() > 4)) {
                    if (debug_mode) {Log.i(TAG,"clause 1");}
                    String data_to_upload = name_txt + ", " + surname_txt + ", " + email_txt + ", "+ cellphone_txt + ", " + postcode_txt + "\n";
                    // upload_to_github(data_to_upload);
                    alert_dialog.dismiss();
                }
            }
        });
    }
});

But this way I get not Button at all. The Alert Dialog only contains the EditText fields.

Marinos K
  • 1,779
  • 16
  • 39
  • Have you read here?: http://stackoverflow.com/a/27345656/2910520. You need to override the button listener to prevent AlertDialog closing itself on choice click, using `dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(...)`. You can remove the showListener because it is not necessary, be carefull that you need to `create()` and `show()`the dialog before changing the button listener – MatPag Apr 12 '17 at 15:01
  • that's what I do in the second code fragment, but this way I don't get any button at all - I reckon because I'm using a `LinearLayout` but I'm lost here. – Marinos K Apr 12 '17 at 15:05

1 Answers1

1
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {

and

Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);

both are different buttons.

try

Button b = alert.getButton(AlertDialog.BUTTON_NEUTRAL);

Sush
  • 3,864
  • 2
  • 17
  • 35