简体   繁体   中英

Error Messages not showing inside Alert Dialog Box

I would like to show an error message within an alert dialog box for password reset, however no error message shows inside the dialog pop up.

Alert Dialog Box

When clicking "Reset" the dialog box will close. However, entering in a valid email does show the toast message "Reset Email Sent"

The validations are 1) leaving the email address empty, 2)not giving a proper email

Login.java

//onclick for forgot password
    forgotPText = findViewById(R.id.forgotPassText);
    forgotPText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //start alert dialog
            View view = inflater.inflate(R.layout.reset_popup,null);
            reset_alert.setTitle("Forgot Password ?")
                    .setMessage("Enter Email For Password Reset Link.")
                    .setPositiveButton("Reset", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //validate the email address is not empty or not valid email
                            EditText email = view.findViewById(R.id.reset_popup_Email);
                            String emailChar = email.getText().toString();
                            if (email.getText().toString().isEmpty()){
                                email.setError("Required Field!");
                                return;
                            }
                            if(!Patterns.EMAIL_ADDRESS.matcher(emailChar).matches()){
                                email.setError("Please provide valid email!");
                                email.requestFocus();
                                return;
                            }
                            //send reset link
                            firebaseAuth.sendPasswordResetEmail(email.getText().toString())
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    //FirebaseUser user = firebaseAuth.getCurrentUser();
                                    //updateUI(user);
                                    Toast.makeText(LoginActivity.this
                                            , "Reset Email Sent", Toast.LENGTH_SHORT).show();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(LoginActivity.this, e.getMessage(),
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }).setNegativeButton("Cancel",null)
                    .setView(view)
                    .create().show();
        }
    });

Use this code:

forgotPText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start alert dialog
                LayoutInflater inflater = getLayoutInflater();
                AlertDialog.Builder reset_alert = new AlertDialog.Builder(MainActivity.this);
                View view = inflater.inflate(R.layout.reset_popup,null);
                reset_alert.setView(view);

                EditText email = view.findViewById(R.id.email);
                TextView tv_cancel = view.findViewById(R.id.tv_cancel);
                TextView tv_submit = view.findViewById(R.id.tv_submit);

                tv_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() { alertDialog.dismiss(); }}, 200);
                    }
                });

                email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        }
                    }
                });

                tv_submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


                        String emailChar = email.getText().toString();


                        if (email.getText().toString().isEmpty()){
                            email.setError("Required Field!");
                             return;
                        }

                         if (!Patterns.EMAIL_ADDRESS.matcher(emailChar).matches()){
                            email.setError("Please provide valid email!");
                            email.requestFocus();
                             return;
                         }
                        //send reset link
                        firebaseAuth.sendPasswordResetEmail(email.getText().toString())
                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {
                                        //FirebaseUser user = firebaseAuth.getCurrentUser();
                                        //updateUI(user);
                                        Toast.makeText(LoginActivity.this
                                                , "Reset Email Sent", Toast.LENGTH_SHORT).show();
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(LoginActivity.this, e.getMessage(),
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

                alertDialog = reset_alert.create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();

            }
        });


////////   reset_popup.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/reset_alert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Forgot Password?"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/des"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:textColor="@color/black"
        android:text="Enter Email For Password Reset Link."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reset_alert" />


    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="Email Address"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/des" />


    <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Cancel"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_submit"
        app:layout_constraintTop_toBottomOf="@+id/email" />


    <TextView
        android:id="@+id/tv_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_marginEnd="353dp"
        android:text="Reset"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email" />


</androidx.constraintlayout.widget.ConstraintLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM