Inflated layout to alertdialog doesn't work

I have the following method where I must confirm the user's password before he is allowed to access the Setup menu. To do so, I make him login again because Parse (my cloud storage) doesn't allow me to confirm passwords. So far so good. The problem is when I inflate the view to my AlertDialog to use a custom button and EditText. You can see in my code below that I have commented the Button section, because when I try to get the onClickListener it doesn't work. It just does nothing. So I'm currently using the alertDialogPositiveButton and it's ok. The second problem and the real one is that I cannot retrieve the data insert into the editText. The SUpassword3is always equals to blank, not null. Can someone please help?

method:

public void confirmPassword(){

        final LayoutInflater inflateSetup = getLayoutInflater();

        AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
        alertSetup.setView(inflateSetup.inflate(R.layout.inflate_setup, null));
        alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("User has clicked the [confirm] button");
                View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);
                edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
                SUpassword3 = edtPassword3.getText().toString();

                final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
                ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if(e==null){
                            //password confirmed, logout last user and go to next setup
                            System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, logout last user and go to setup");
                            beforeConfirmPassword.logOut();
                        }else{
                            //passwords don't match
                            System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]");
                        }
                    }
                });
            }
        });
        final AlertDialog dialogSetup = alertSetup.create();
        dialogSetup.show();


/**
        btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm);
        btnConfirmPassowod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                System.out.println("User has clicked the [confirm] button");
                final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser();
                ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if(e==null){
                            //password confirmed, logout last user and go to next setup
                            System.out.println("Password confirmed, logout last user and go to setup");
                            beforeConfirmPassword.logOut();
                        }else{
                            //passwords don't match
                            dialogSetup.dismiss();
                            Snackbar.make(v, "Wrong password, try again.", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                        }
                    }
                });
            }
        }); */
    }

inflate_setup.xml(the xml file I inflate on the alertDialog)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/edtPassword3"
        android:layout_gravity="center_horizontal"
        android:hint="@string/EN_txt.confirmPassword"
        />
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/btnConfirm"
    android:layout_gravity="center_horizontal" />

</LinearLayout>

Final version(working):

public void confirmPassword(){

        final LayoutInflater inflateSetup = getLayoutInflater();
        final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null);

        AlertDialog.Builder alertSetup = new AlertDialog.Builder(this);
        alertSetup.setView(viewInflate_setup);
        alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("User has clicked the [confirm] button");

                edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
                SUpassword3 = edtPassword3.getText().toString();

...

Upvotes: 0

Views: 1446

Answers (3)

Shaishav
Shaishav

Reputation: 5312

Get a reference of the inflated view:

View dialogView = inflateSetup.inflate(R.layout.inflate_setup, null);

In dialog use:

alertSetup.setView(dialogView);

Then, access button using:

Button myButton = (Button) dialogView.findViewById(R.id.btnConfirm);

Upvotes: 0

Divers
Divers

Reputation: 9569

You inflate another view inside your onClickListener. That's why you actually check text of another edtPassword3, not that one which visible for user. It should be something like that:

View view = inflateSetup.inflate(R.layout.inflate_setup;
final EditText edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3);
alertSetup.setView(view, null));
  alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override 
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("User has clicked the [confirm] button");
                SUpassword3 = edtPassword3.getText().toString(); 
 ....

Upvotes: 1

sonnv1368
sonnv1368

Reputation: 1597

Try to use: Dialog class

// custom dialog
  final Dialog dialog = new Dialog(context);
  dialog.setContentView(R.layout.custom);
  dialog.setTitle("Title...");

Upvotes: 0

Related Questions