How to fix TextInputLayout passwordToggle?

197 views Asked by At

I created a simple login page with validations but i have a problem in the password section when i want to use passwordToggleEnabled the app crash and don't open no idea why since i have 0 errors.

My project in on API 26.

I have added in build gradle inside dependicies:

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'

And inside the XML file this is my code of password input:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/test2"
            android:layout_below="@id/usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            app:errorEnabled="true"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

and in MainActivity.java this is how i am calling the id of pwd:

String passwordInput = test2.getText().toString().trim();

So this is how am implementing my code and the app keeps stopping so what i am doing wrong ?

Thank you!

3

There are 3 answers

3
Gabriele Mariotti On BEST ANSWER

Use the method getEditText to get the EditText used for text input:

TextInputLayout test2= findViewById(R.id.test2);
String passwordInput = test2.getEditText().getText().toString().trim();
0
Somesh Kumar On

You have to first find the view by assigning it an ID like this

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

and then find it in your activity like this

 TextInputEditText etPassword= findViewById(R.id.etPassword);

then you can get the entered text like

String passwordInput = etPassword.getText().toString().trim();
2
Kanwarpreet Singh On

Assigning an ID to TextInputEditText

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

and then find it in your activity like this

EditText etPassword= findViewById(R.id.etPassword);

then you can get the entered text like

String mPassword = etPassword.getText().toString().trim();