Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing multiple AlertDialogs

I am relatively new to Android/Java. Thanks to Stack Overflow, I was able to learn a lot from the questions asked here. However, I am now stuck on this problem.

I have a password input AlertDialog that pops up when we start the app. It reads the password from the EditText and compare this with the one stored in a file. I needed an extra AlertDialog which show up when an invalid/wrong password is provided. This also I implemented.

Now, this second dialog has two buttons - Reset and Retry. I want the activity to show the first dialog again when we click Retry. This is where I am clueless. If anyone can provide a working solution and a little explanation, I will be very obliged.

Here is my code:

LayoutInflater li = LayoutInflater.from(context);
View passView = li.inflate(R.layout.authdialog, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(passView);

final EditText passInput = (EditText) passView.findViewById(
        R.id.editTextDialogUserInput);
final TextView txtv = (TextView) findViewById(R.id.textv);

// set dialog message
alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {
                String data = "";
                try {
                    FileInputStream fis = openFileInput("authfile");

                    InputStreamReader in = new InputStreamReader(fis);
                    BufferedReader br = new BufferedReader(in);
                    data = br.readLine();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (data.toString().equals(
                        passInput.getText().toString())) {
                    txtv.setText("You Have Logged in");
                } else {
                    LayoutInflater ln = LayoutInflater.from(context);
                    View invalidView = ln.inflate(R.layout.invdialog,
                            null);
                    AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(
                            context);
                    invalidDialogBuild.setView(invalidView);

                    // set dialog message
                    invalidDialogBuild
                            .setCancelable(false)
                            .setPositiveButton(
                                    "Retry",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface dialog,
                                                int id) {
                                            dialog.cancel();
                                        }
                                    })
                            .setNegativeButton(
                                    "Reset",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface dialog,
                                                int id) {
                                            dialog.cancel();
                                        }
                                    });

                    dialog.cancel();

                    AlertDialog invalidDialog = invalidDialogBuild
                            .create();

                    // show it
                    invalidDialog.show();
                }
            }
        })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
like image 815
GunJack Avatar asked Nov 03 '22 03:11

GunJack


2 Answers

Create two separate method of your dialogs and call them as i have done in below code check out.

 private void dialogShow()
{
    LayoutInflater li = LayoutInflater.from(context);
    View passView = li.inflate(R.layout.authdialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(passView);
    final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput);
    final TextView txtv = (TextView) findViewById(R.id.textv);
    // set dialog message
    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener()
            {
            public void onClick(DialogInterface dialog, int id)
            {
                String data = "";
                try
                {
            FileInputStream fis = openFileInput("authfile");                            InputStreamReader in = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(in);
                data = br.readLine();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            if (data.toString().equals(passInput.getText().toString()))
                    {
                        txtv.setText("You Have Logged in");
                    }
                    else
                    {
                    RetryDialog();//Call the dialog of retry
                    }
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.cancel();
                }
            });
    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    // show it
    alertDialog.show();
}
    //Shows the Retry Dialog 
private void RetryDialog()
{
     LayoutInflater ln = LayoutInflater.from(context);
     View invalidView = ln.inflate(R.layout.activity_main, null);
    AlertDialog.Builder invalidDialogBuild = new AlertDialog.Builder(context);
    // invalidDialogBuild.setView(invalidView);
    // set dialog message
    invalidDialogBuild.setCancelable(false)
            .setPositiveButton("Retry", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialogShow();
                    dialog.cancel();
                }
            }).setNegativeButton("Reset", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.cancel();

                }
            });

    AlertDialog invalidDialog = invalidDialogBuild.create();
    // show it
    invalidDialog.show();

}

EDITED:

Dialog with the ThreeButtons.

  /**
 * This is method to display dialog with three
 * button("Yes, No and Cancel button")
 */
public void showThreeButtonDialog()
{
    private AlertDialog m_alertDialog;
    private AlertDialog.Builder m_builder = new AlertDialog.Builder(this);
    m_builder.setTitle("Title Text");
    m_builder.setMessage("Dialog Message");
    m_builder.setPositiveButton("yes", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface p_dialog, int p_which)
        {
            Toast.makeText(AlertDialogActivity.this, "Press Yes", Toast.LENGTH_SHORT).show();
        }
    });
    m_builder.setNeutralButton("No"), new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface p_dialog, int p_which)
        {
            Toast.makeText(AlertDialogActivity.this, "Press No", Toast.LENGTH_SHORT).show();
        }
    });
    m_builder.setNegativeButton("Cancel"), new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface p_dialog, int p_which)
        {
            Toast.makeText(AlertDialogActivity.this, "Press Cancel", Toast.LENGTH_SHORT).show();
        }
    });
    m_alertDialog = m_builder.create();
    m_alertDialog.show();
}
like image 111
GrIsHu Avatar answered Nov 15 '22 00:11

GrIsHu


Grishu beat me to it, mine is at least shorter...

private void showLoginDialog(final Context context) {
    LayoutInflater li = LayoutInflater.from(context);
    View passView = li.inflate(R.layout.authdialog, null);
    final EditText passInput = (EditText) passView.findViewById(R.id.editTextDialogUserInput);
    final TextView txtv = (TextView) findViewById(R.id.textv);

    new AlertDialog.Builder(context)
        .setView(passView)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                String data = "";
                try {
                    FileInputStream fis = openFileInput("authfile");
                        InputStreamReader in = new InputStreamReader(fis);
                    BufferedReader br = new BufferedReader(in);
                    data = br.readLine();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (data.toString().equals(passInput.getText().toString())) {
                    txtv.setText("You Have Logged in");
                } else {
                    dialog.dismiss();
                    showInvalidDialog(context);
                }
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        })
        .show();
}

private void showInvalidDialog(final Context context) {
    LayoutInflater ln = LayoutInflater.from(context);
    View invalidView = ln.inflate(R.layout.invdialog, null);

    new AlertDialog.Builder(context)
        .setView(invalidView)
        .setCancelable(false)
        .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                showLoginDialog(context);
            }
        })
        .setNegativeButton("Reset", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        })
        .show();
}

Explanation: move the code for the two dialogs into separate methods which can call each other. Make consequent use of the builder pattern to keep the code short and elegant. Use dialog.dismiss() instead of dialog.cancel() unless you want a potential OnCancelListener to be called.

like image 33
Emanuel Moecklin Avatar answered Nov 14 '22 23:11

Emanuel Moecklin