Search code examples
androidpasswordstextview

Get Text from Android Password TextView


I hope this hasn't been asked yet, since I couldn't find it. I am attempting to get a user's username and password for an online service. I created a username TextView and a password TextView. I'm able to get text out of the username TextView without problems. However, I can't get anything from the password TextView. Below is the XML:

<TextView
    android:id="@+id/user_password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="8dp"
    android:text="@string/passcodeQuery"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:hint="@string/passwordHint"
    android:inputType="textPassword" />

The problem is, if I run the code

TextView passField = (TextView)findViewById(R.id.user_password);

then

String toastMessage = "Password: " + passField.getText().toString();

No matter what I put into the password field, the toast message gives me

Password: Password

I obviously won't be toasting the users password normally, I am just doing it now because I want to make sure I'm reading the inputs correctly. Unfortunately, that doesn't seem to be the case, as I can't read the password field. How do you get around that? Ton's of apps seem to be able to do it. Is there an easier way to get a password than what I'm doing?


Solution

  • If you want to Toast like "PassWord: YOUR EDITTEXT VALUE" then use this.

    EditText passField = (EditText)findViewById(R.id.editText2); 
    

    and then use:

    String toastMessage = "Password: " + passField.getText().toString();
    

    Now toast the message.