1

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SmithBZ
  • 125
  • 1
  • 9

3 Answers3

1

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();
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Thank you ! My mistake was i didn't assign an id for the EditText now its solved but got a simple problem is the toggle and the error message appear very close to each other how can i fix this ? https://i.imgur.com/zGlDJ2h.png @Gabriele Mariotti – SmithBZ Oct 04 '19 at 10:05
  • @SmithBZ This is a separate problem. It has nothing to do with your question. You may wish to consider asking a separate question with the code used and a screen to help the community. Also consider to migrate the design library to the [material components library](https://github.com/material-components/material-components-android). – Gabriele Mariotti Oct 04 '19 at 10:16
  • Yes you're right.Anyway thank you for your help much appreciated @Gabriele Mariotti – SmithBZ Oct 04 '19 at 10:20
1

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();
Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49
0

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();
Kanwarpreet Singh
  • 2,037
  • 1
  • 13
  • 28