EditTextPreference does not mask password even with android:inputType="textPassword"
Asked Answered
G

1

8

I have following code

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <EditTextPreference
            app:key="pref_password"
            app:title="Password"
            app:iconSpaceReserved="false"
            app:dialogTitle="Password"
            android:inputType="textPassword"/>

</androidx.preference.PreferenceScreen>

But the edit text field is not masked with dots even with android:inputType="textPassword"

I am using androidx. Anyone please help

Update

I tried following as a commenter suggested, but no luck

<EditTextPreference
            android:key="pref_password"
            android:title="Password"
            app:iconSpaceReserved="false"
            android:dialogTitle="Password"
            android:inputType="textPassword"/>
Goofball answered 13/7, 2019 at 11:45 Comment(5)
instead of " app: ", go with " android: ". Link, #6164930Luxuriance
@VirajS tried, but no luck. updated the questionGoofball
Sorry about the constant rollbacking! There was a weird problem with the rollback button. (I intended to roll back the question to before the kotlin tag was added as this tag is not relevant to the question)Surveillance
@Surveillance actually I was using kotlinGoofball
Just because you’re using Kotlin doesn’t mean you can add the kotlin tag. Tags should only be added if they are a topic in the question itself: stackoverflow.com/help/taggingSurveillance
A
12

Setting attributes directly on the EditTextPreference doesn't work with the AndroidX library - since an EditTextPreference isnt' an EditText, and shouldn't work as such. Instead you should use an OnBindEditTextListener to customize the EditText when it is displayed. (need androidx.preference:preference v1.1.0 and higher)

See the settings guide for more information

edit with code:

Java:

EditTextPreference preference = findPreference("pref_password");

if (preference!= null) {
    preference.setOnBindEditTextListener(
            new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            });
}

Kotlin:

val editTextPreference: EditTextPreference? = findPreference("pref_password")

        editTextPreference?.setOnBindEditTextListener {editText ->  
            editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
Allhallowmas answered 15/7, 2019 at 1:17 Comment(1)
can you add code snippet used to mask text also so answer looks more completeGoofball

© 2022 - 2024 — McMap. All rights reserved.