9

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"/> 
5
  • instead of " app: ", go with " android: ". Link, stackoverflow.com/questions/6164430/… Commented Jul 13, 2019 at 13:05
  • @VirajS tried, but no luck. updated the question Commented Jul 13, 2019 at 14:21
  • 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) Commented Jul 15, 2019 at 4:12
  • 1
    @Edric actually I was using kotlin Commented Jul 15, 2019 at 4:21
  • 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/tagging Commented Jul 15, 2019 at 4:30

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

can you add code snippet used to mask text also so answer looks more complete

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.