androidandroid-edittextandroid-inputtypeandroid-singleline

Calling setSingleLine on an EditText with inputType=textPassword make characters visible


I put an EditText with inputType="textPassword" in my activity's XML

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:inputType="textPassword"
        android:padding="16dp"
        android:id="@+id/passwordInput"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Till now there is not any problem and I see circles intead of real password characters:

enter image description here

The interesting part is here. Now if I call setSingleLine() on the EditText in the Activity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        passwordInput.setSingleLine()
    }
}

will see that the password characters is surprisingly visible!

enter image description here

Another interesting thing is that this issue will not happen if I put android:singleLine="true" in XML of the EditText.

Note: I know that setting setSingleLine on a password field is useless, but I'm curious why calling this function has such side effect.


Solution

  • I think it is because when you call setSingleLine, textview will change it transformation method from PasswordTransformationMethod to SingleLineTransformationMethod. there is only one transformation method accepted at the time in EditText (which is child of TextView)

    you can check the source code here: setSingleLine() https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#6727

    follow through the code fill call function setTransformationMethod

    https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#1461