How to make password change prompt confirmation in SettingsActivity for Android?

3.6k views Asked by At

I have a Settings Activity in Android Studio and a password List item that when clicked prompts an EditTextPreference dialogue with InputType password. How can I make it so that when the user enters the password, another Dialog pops up asking the user to confirm the password change.

Or even better, how can I make the EditTextPreference multi line and prompt for old password, new password, confirm new password within the same Dialog?

I added the following in my main activity's onCreate

ListView lv;
Context ctx=this;

onCreate() {



lv = getListView();

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (position == 1) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctx);
                alertDialog.setTitle("Values");
                final EditText oldPass = new EditText(ctx);
                final EditText newPass = new EditText(ctx);
                final EditText confirmPass = new EditText(ctx);


                oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());


                LinearLayout ll = new LinearLayout(ctx);
                ll.setOrientation(LinearLayout.VERTICAL);

                ll.addView(oldPass);

                ll.addView(newPass);
                ll.addView(confirmPass);
                alertDialog.setView(ll);
                alertDialog.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                alertDialog.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

                AlertDialog alert11 = alertDialog.create();
                alert11.show();
            }
        }
    });
}

But there is no change in the behavior of the app. The listItem I'm focusing on is second from the top so presumably position == 1.

1

There are 1 answers

8
Uma Kanth On BEST ANSWER

I created a LinearLayout and added two textBoxes into it and then gave it to the alertBox.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Values");
final EditText oldPass = new EditText(MainActivity.this);
final EditText newPass = new EditText(MainActivity.this);
final EditText confirmPass = new EditText(MainActivity.this);


oldPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
newPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
confirmPass.setTransformationMethod(PasswordTransformationMethod.getInstance());

oldPass.setHint("Old Password");
newPass.setHint("New Password");
confirmPass.setHint("Confirm Password");
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);

ll.addView(oldPass);

ll.addView(newPass);
ll.addView(confirmPass);         
alertDialog.setView(ll);
alertDialog.setPositiveButton("Yes",
        new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});
alertDialog.setNegativeButton("No",
        new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert11 = alertDialog.create();
alert11.show();

Check the passwords with the object references oldPass and newPass.

If you want to add anymore objects on to it, just create and add to the view.

To deal with the problem that when I cancel or pressed yes in this new dialog, the old dialog would appear, I changed "EditTextPreference" to simply "Preference" in my pref_general.xml file. Now the old dialogue does not appear or show up at all and the problem is fixed.