gregkorossy / Android-Support-Preference-V7-Fix

Android androidx.preference support library has some issues, this lib tries to fix them.
https://discord.gg/87NVsSK
Apache License 2.0
497 stars 46 forks source link

Show password for EditTextPreference? #181

Open Mygod opened 6 years ago

Mygod commented 6 years ago

There is an easy way to do this but it requires Google's material design library. What do you think?

UPDATE: There is another way, which is to always show password, like Wi-Fi AP settings in Android Pie.

gregkorossy commented 6 years ago

I'll look into it.

gregkorossy commented 6 years ago

Technically it'd be possible to add a checkbox to the dialog that shows "Show password" (or whatever the dev sets).

gregkorossy commented 6 years ago

Would this work?

Sample (this is 28.0.0.0-rc01, so don't mind the message styling):

Hidden Visible
device-2018-09-12-171925 device-2018-09-12-171905

I just modified EditTextPreferenceDialogFragmentCompat's onAddEditTextToDialogView(...):

protected void onAddEditTextToDialogView(View dialogView, final EditText editText) {
    View oldEditText = dialogView.findViewById(android.R.id.edit);
    if (oldEditText != null) {
        ViewGroup container = (ViewGroup) (oldEditText.getParent());
        if (container != null) {
            container.removeView(oldEditText);
            container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            final int inputType = editText.getInputType();
            AppCompatCheckBox chkBox = new AppCompatCheckBox(container.getContext());
            chkBox.setText("Show password");
            chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    final int selStart = editText.getSelectionStart();
                    final int selEnd = editText.getSelectionEnd();

                    if (isChecked) {
                        editText.setInputType(inputType | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    } else {
                        editText.setInputType(inputType);
                    }

                    editText.setSelection(selStart, selEnd);
                }
            });
            container.addView(chkBox, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }
}
Mygod commented 6 years ago

I prefer the solution in #182 or using Google's material design library. This checkbox looks kinda retro.

Mygod commented 6 years ago

If you insist on using this solution, I would suggest these changes:

gregkorossy commented 6 years ago

I don't know what you mean by "This allows for implementing an EditTextPreference as in Android 9.0's Wi-Fi AP configuration." I opened Pie in the emulator and it shows this screen when adding a new network: screenshot_1536839231

gregkorossy commented 6 years ago

Could you send me a screenshot of the screen in question? The emulator's AP has no security settings, so cannot see what it looks like.

Mygod commented 6 years ago

Oops. Sorry for being unclear, I was referring to Wi-Fi Hotspot settings. Demo

gregkorossy commented 6 years ago

Wouldn't it just work if the user set the inputType attribute (or at least as a flag) to textVisiblePassword? Forcing visible passwords on the users seems a bit odd to me.

Mygod commented 6 years ago

Hmm yeah I guess that works too. I didn't know that flag was a thing.

gregkorossy commented 6 years ago

I think, as an extra, the "Show password" checkbox could be placed in the dialog with an attribute, like pref_showPasswordCheckbox="true". I only need to find translations for "Show checkbox" in this case.

Or, the other option would be to enable it by setting the text for it, like pref_showPasswordText="@string/show_pass_string".

Mygod commented 6 years ago

Why not use app:passwordToggleEnabled?

Mygod commented 6 years ago

Hmm okay I don't see an easy way to inflate and use a TextInputLayout given the current code structure. Ideally I'd like something like this: https://github.com/shadowsocks/shadowsocks-android/pull/1948 (but without that huge layout file, the part that duplicates support lib)

Reopening this...

gregkorossy commented 6 years ago

The problem is that it needs the material components package which is huge. On the other hand, the checkbox solution needs no extra dependencies.

Mygod commented 6 years ago

I'm thinking about an option that allows user to pass in edittext layout file, like app:pref_editTextLayout="@layout/material_password_toggle". What do you think?