atf98
atf98

Reputation: 143

How to check the password entered is right or not?

I tried to return it in dialog layout, but the debug give me null of the variable I'm trying to check if the password is right or not, but it gave me an error of null object

Here is the function where I return the password from the database to know this function is in the main activity and the password confirm is not the context of this activity

public void showDialog(final Intent i){
    LayoutInflater factory = LayoutInflater.from(this);
    final View deleteDialogView = factory.inflate(R.layout.password_confirm, null);
    final AlertDialog deleteDialog = new AlertDialog.Builder(this).create();
    deleteDialog.setView(deleteDialogView);
    deleteDialogView.findViewById(R.id.confirmPasswordCheck).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            db.collection("Departments").document(DeparmentName).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
                    EditText editText = findViewById(R.id.edit_password_check);
                    passwordReturn = task.getResult().getString("Password");
                    if(editText.getText().equals(passwordReturn)){
                        startActivity(i);
                    }else{
                        textInputLayout.setError("Password is wrong");
                    }
                }
            });
        }
    });
    deleteDialogView.findViewById(R.id.cancelPasswordCheck).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteDialog.dismiss();
        }
    });

    deleteDialog.show();
}

Here is the where i call the function and what i send to it

        Intent i = new Intent(MainActivity.this, AddPatientActivity.class);
        i.putExtra("SuperSession", SuperKey);
        i.putExtra("DepartmentName", DeparmentName);
        showDialog(i);

and this is the layout

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".MainActivity"
    android:background="@color/blueState">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txt_password_confirm"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textSize="24sp" />
    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_password_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:theme="@style/MyEditText"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/MyErrorText"
        app:passwordToggleEnabled="true">

        <EditText
            android:id="@+id/edit_password_check"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/century_gothic"
            android:hint="@string/password"
            android:imeOptions="flagNoExtractUi"
            android:inputType="textPassword"
            android:theme="@style/MyEditText" />
    </android.support.design.widget.TextInputLayout>
        <Button
            android:id="@+id/confirmPasswordCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_green_btn"
            android:text="Confirm"/>
        <Button

            android:id="@+id/cancelPasswordCheck"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_red_btn"
            android:text="@string/cancelBtn"/>
</LinearLayout>

Upvotes: 1

Views: 41

Answers (1)

Beyazid
Beyazid

Reputation: 1835

You should declare view bindings before setOnClickListener().

TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
EditText editText = findViewById(R.id.edit_password_check);

Then you should use editText.getText().toString().equals(passwordReturn) instead of editText.getText().equals(passwordReturn) . Because editText.getText() returns Editable

Upvotes: 1

Related Questions