Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show password for EditTextPreference? #181

Open
Mygod opened this issue Sep 8, 2018 · 15 comments
Open

Show password for EditTextPreference? #181

Mygod opened this issue Sep 8, 2018 · 15 comments
Assignees

Comments

@Mygod
Copy link
Contributor

Mygod commented Sep 8, 2018

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
Copy link
Owner

I'll look into it.

@gregkorossy
Copy link
Owner

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

@gregkorossy
Copy link
Owner

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
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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

@Mygod
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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

  • Use XML to inflate the surroundings;
  • Use layout params from oldEditText.

@gregkorossy
Copy link
Owner

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
Copy link
Owner

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
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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

@gregkorossy
Copy link
Owner

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
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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

@Mygod Mygod closed this as completed Sep 13, 2018
@gregkorossy
Copy link
Owner

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
Copy link
Contributor Author

Mygod commented Sep 13, 2018

Why not use app:passwordToggleEnabled?

@Mygod
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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: shadowsocks/shadowsocks-android#1948 (but without that huge layout file, the part that duplicates support lib)

Reopening this...

@Mygod Mygod reopened this Sep 13, 2018
@gregkorossy
Copy link
Owner

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
Copy link
Contributor Author

Mygod commented Sep 13, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants