Displaying the password in an EditText when the current inputType is set to ‘textPassword’

Feedback


Question:

Although there have been numerous similar questions, none of them have provided a solution that works for me. My requirement is to have the password visible when the box is checked and for it to appear as dots when unchecked. However, currently, it only displays as dots.

I am encountering a nullPointerException.

07-08 15:08:50.441: E/AndroidRuntime(4518): FATAL EXCEPTION: main
07-08 15:08:50.441: E/AndroidRuntime(4518): java.lang.NullPointerException
07-08 15:08:50.441: E/AndroidRuntime(4518):     at com.passwordkeeper.ui.ActivityPreferenceScreen$1.onPreferenceClick(ActivityPreferenceScreen.java:65)
07-08 15:08:50.441: E/AndroidRuntime(4518):     at android.preference.Preference.performClick(Preference.java:951)
07-08 15:08:50.441: E/AndroidRuntime(4518):     at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)

This is my code:

@SuppressWarnings("deprecation")
public void setFields(){
    mPasswordField = (EditText) findViewById (R.id.loginPwdEditText);
    showPwd = (CheckBoxPreference) findPreference("checkBox");
    showPwd.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            if(showPwd.isChecked()){        
                Log.v("showpwd", "apparently its checked");
                mPasswordField.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            else{
                Log.v("showpwd", "apparently its not checked now");
                mPasswordField.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
                    }
            return true;
        }
    });

The log is functional as it prints “apparently its checked” when the box is checked. However, it subsequently becomes unresponsive and the null
pointer exception
causes the app to shut down.

Any help would be useful!



Solution 1:

This code is wrong:

mPasswordField.setInputType(129);
mPasswordField.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);

To replace the first set with the second one, the flags need to be combined using the OR operation.

mPasswordField.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );

In terms of NPE, it is highly probable that you will conclude with

mPasswordField

Since it is

null

, it is an exception.


Solution 2:

To display password:

 if(showPwd.isChecked()){
    if(mPasswordField!=null)
       pass.setInputType(InputType.TYPE_CLASS_TEXT);
}
else
    pass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);


Solution 3:

Try to move

mPasswordField = (EditText) findViewById (R.id.loginPwdEditText); 

inside

onPreferenceClick(Preference preference)

method


Solution 4:

Simply add this single line of code to your check condition.

btnCheckBox.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            editPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    });

Frequently Asked Questions