Darius Radius
Darius Radius

Reputation: 189

Hide password on settings screen

I have easy problem, but I cannot find solution. Lets have settings page generated over Android studio. And here is one field - password.

<EditTextPreference
    android:defaultValue="@string/pref_default_display_password"
    android:inputType="textPassword"
    android:key="password"
    android:maxLines="1"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:password="true"
    android:title="@string/pref_title_display_password" />

There is no problem with input which is with asterisks. But problem is, that I see saved password on screen:

enter image description here

How can I hide it?

Thank you very much.

Upvotes: 6

Views: 2785

Answers (5)

Seongki Ahn
Seongki Ahn

Reputation: 1

I resolved it as below.

resolved code
getEditText().setInputType(
    TYPE_CLASS_NUMBER|
    TYPE_NUMBER_VARIATION_PASSWORD|
    TYPE_NUMBER_FLAG_DECIMAL);

related code

TextView class
private static boolean isPasswordInputType(int inputType) {
    int variation = inputType & 4095;
    return variation == 129 || variation == 225 || variation == 18;
}

variation == 18 -> 
public static final int TYPE_TEXT_VARIATION_URI = 0x00000010;
public static final int TYPE_CLASS_NUMBER = 0x00000002;

Upvotes: 0

Mohammad irshad sheikh
Mohammad irshad sheikh

Reputation: 888

public class PasswordPreference extends DialogPreference

{

private String password;
private EditText passwordEditText;
private EditText confirmEditText;

public PasswordPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    init(context, attrs);
}

public PasswordPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    setDialogLayoutResource(R.layout.confirm_password);

    passwordEditText = new EditText(context, attrs);
    confirmEditText = new EditText(context, attrs);

}

private void detachView(View view, View newParent) {
    ViewParent oldParent = view.getParent();
    if (oldParent != newParent && oldParent != null)
        ((ViewGroup) oldParent).removeView(view);
}

@Override
public void onBindDialogView(View view) {
    detachView(passwordEditText, view);
    detachView(confirmEditText, view);

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearLayout);
    layout.addView(passwordEditText);
    layout.addView(confirmEditText);

    passwordEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            verifyInput();
        }
    });

    confirmEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            verifyInput();
        }
    });

    String oldPassword = getSharedPreferences().getString("password", "");
    passwordEditText.setText(oldPassword);
    confirmEditText.setText(oldPassword);
}

private void verifyInput() {
    String newPassword = passwordEditText.getText().toString();
    String confirmedPassword = confirmEditText.getText().toString();

    boolean passwordOk = false;
    if (newPassword.equals(confirmedPassword)) {
        passwordOk = true;
        password = newPassword;
    }

    AlertDialog dialog = (AlertDialog) getDialog();
    if (dialog != null)
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(passwordOk);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (!positiveResult)
        return;
    persistString(password);
}

}

    <com.abax.applocker.PasswordPreference
        android:background="@color/white"
        android:inputType="numberPassword"
        android:key="password"
        android:password="true"
        android:summary="Edit the Unlock password"
        android:title="Edit Password" />

use this

Upvotes: 0

alextk
alextk

Reputation: 6239

I solved the issue using OnPreferenceChangeListener which is called before the preference is displayed and when it is changed. I take the opportunity then to set a modified version of the password in the summary, by converting the text to string with stars

Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        ...
        if (preference instanceof EditTextPreference){
            // For the pin code, set a *** value in the summary to hide it
            if (preference.getContext().getString(R.string.pref_pin_code_key).equals(preference.getKey())) {
                stringValue = toStars(stringValue);
            }
            preference.setSummary(stringValue);
        }
        ...
        return true;
    }
};

String toStars(String text) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        sb.append('*');
    }
    text = sb.toString();
    return text;

}

Upvotes: 8

Darius Radius
Darius Radius

Reputation: 189

thank you all, currently I used "hotfix", which hide value of password. I will try replace it in the future. Hotfix is, that I simply remove showing of password:

bindPreferenceSummaryToValue(findPreference("username"));
//bindPreferenceSummaryToValue(findPreference("password"));
bindPreferenceSummaryToValue(findPreference("ip"));
bindPreferenceSummaryToValue(findPreference("port"));

Upvotes: 2

Mohammad irshad sheikh
Mohammad irshad sheikh

Reputation: 888

use this

<EditTextPreference
    android:inputType="textPassword"
    android:key="@string/key"
    android:summary="@string/summary"
    android:title="@string/title" />

android:inputType as you want

thanks

Upvotes: 0

Related Questions