1

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.

Alex
  • 325
  • 7
  • 16

1 Answers1

2

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.

Alex
  • 325
  • 7
  • 16
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • So for this solution would I have to override the onItemClickListener for the settings listview to launch this dialogue rather than the default one given? – Alex Jun 12 '15 at 05:30
  • Yes, you can use whereever you want to. – Uma Kanth Jun 12 '15 at 05:33
  • 1
    in `onItemClickListener` get the password from the `Listitem` and set it to oldPassword if you want, show the dialog box – Uma Kanth Jun 12 '15 at 05:35
  • Ok I'm trying it out. The template android studio provides for a Settings Activity doesn't even include an explicit listener I guess (may be wrong) so I have to write it out myself. – Alex Jun 12 '15 at 05:36
  • OK thanks I got it working, but when I press "No" the old dialogue pops up even though I have (at)Override before my click listener. Will this just be bypassed when I add my code to replace dialog.cancel() or is there a way I can completely override that original dialogue? – Alex Jun 12 '15 at 06:22
  • remove the old dialog before this and keep it in the yes button click event. – Uma Kanth Jun 12 '15 at 06:33
  • It's not an explicit dialogue though that I can just remove because this is the settings activity template provided by android studio. Or is there an easy way I can do this? – Alex Jun 12 '15 at 06:38
  • I don't know about that. I'm sorry. You could post a question about that. someone else experienced in that could definitely help – Uma Kanth Jun 12 '15 at 06:47