0

There doesn't appear to be a specific password text dialog in the Androidx (or Android) library.

I want to add a button so that the user can switch between text view and password text view (asterisks instead of letters) for this preference even though, as someone might want to tell me, it's not a fabulous idea to store passwords as preferences. Eventually I'll have a more robust approach but in the meantime this is what I've got.

I'm using the code that Android Studio (generously) offers me for "Preference Activity". In all other respects it seems pretty good, and better than I can manage myself yet. It's just got this (annoying lack of) feature.

This question is a little too old to reference Androidx, and according to the (main) relevant answer to my context, I can't use AndroidX here. However, using the code from the Settings Activity I don't explicitly mention DialogPreference at all.

So, is there a way to slot in a "reveal" button in this situation, or should I either not use the "textPassword" input type, or completely rebuild this activity?

Markers
  • 328
  • 1
  • 13
  • https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout has an "end icon mode" capability that you can set to password mode, which will then let you toggle whether the text is masked. – Ben P. Feb 11 '20 at 17:40

1 Answers1

0

I was messing round with something similar the other day. I didn't use a reveal button, but just got it to never show the password:

input_password.setText(prefs.getYourPassword().toAsterix())

private fun String.toAsterix(): String {
   return replace("[.]", "*")
}

With a PreferenceActivity, you would have to make a custom view. It would be an EditText and a Button. Clicking the button would set the text to either prefs.getYourPassword() or prefs.getYourPassword().toAsterix().

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks for that @Blundell. The issue appears to be "injecting" that type of button into an EditTextPreference view (as defined in my fragment layout xml file, though could've been in code, I s'pose). – Markers Feb 11 '20 at 17:50
  • Yeah, it's a bit of effort but take a look at this: https://stackoverflow.com/questions/16108609/android-creating-custom-preference/33842737 – Blundell Feb 11 '20 at 17:54
  • @Markers If you found this helpful pls remember to upvote or mark it as answered https://stackoverflow.com/help/someone-answers – Blundell Feb 15 '20 at 23:02