Catalin Ghita
Catalin Ghita

Reputation: 826

How to recreate an alert dialog

I am trying to build an alert dialog that pops on the screen if the current user has no location details.

Also I would to like to check if the user has filled both EditTexts and if not, I would like to recreate the alert dialog preventing the user to advance further.

Is there a simple way of restarting the alert?

Alert Dialog

    AlertDialog.Builder alert = new AlertDialog.Builder(UserActivity.this, R.style.MyDialogTheme);
    LayoutInflater inflater = UserActivity.this.getLayoutInflater();
    View layout = inflater.inflate(R.layout.no_location_alert_layout, null);
    alert.setView(layout);

    final EditText userCountryInput = (EditText) layout.findViewById(R.id.alert_country_edit_text);
    final EditText userCityInput = (EditText) layout.findViewById(R.id.alert_city_edit_text);

    alert.setTitle("Enter your details");
    alert.setMessage("Oops! Looks like you haven't added any current location details.");
    alert.setCancelable(false);

    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String userCountry = userCountryInput.getText().toString();
            String userCity = userCityInput.getText().toString();

            if (userCountry.isEmpty() && userCity.isEmpty()) {
                Toast.makeText(context, "Please enter both your current city and country", Toast.LENGTH_LONG).show();
                // HERE I NEED TO RESTART THE ALERT
            }
            else
            {
            // DO SOME STUFF
            }
        }
    });
    alert.show();

Adding alert.show() results in this error:

 E/UncaughtException: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4498)
    at android.view.ViewGroup.addView(ViewGroup.java:4339)
    at android.view.ViewGroup.addView(ViewGroup.java:4311)
    at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
    at android.support.v7.app.AlertController.setupView(AlertController.java:463)
    at android.support.v7.app.AlertController.installContent(AlertController.java:226)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:436)
    at android.app.Dialog.show(Dialog.java:314)
    at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
    at com.example.android.booktrade_v10.ui.searchFeature.UserActivity$5.onClick(UserActivity.java:279)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

Upvotes: 1

Views: 1431

Answers (2)

Catalin Ghita
Catalin Ghita

Reputation: 826

I have found a solution to restrict user access before pressing the done button by actually disabling the button according to one's needs( E.g. requesting user to type some info into editText) so therefore eliminating the need of restarting the dialog.

This is an example code below to disable a positive or negative button of an alert dialog

    final AlertDialog.Builder alert = new AlertDialog.Builder(UserActivity.this, R.style.MyDialogTheme);
    LayoutInflater inflater = UserActivity.this.getLayoutInflater();
    View layout = inflater.inflate(R.layout.no_location_alert_layout, null);
    alert.setView(layout);


    alert.setTitle("Enter your details");
    alert.setMessage("Oops! Looks like you haven't added any current location details.");
    alert.setCancelable(false);


    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
             doSomething();
          }
    });
       // CREATE THE ACTUAL DIALOG
    final AlertDialog dialog = alert.create();
    dialog.show();

  // MANIPULATE BUTTON

    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE)
            .setEnabled(false);

        if(constraint){
                        ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE)
                        .setEnabled(true);
        }


}

Upvotes: 1

Chris Stillwell
Chris Stillwell

Reputation: 10547

Just call alert.show(); to show the alert again.

Upvotes: 0

Related Questions