Reputation: 2975
I want to prevent user from viewing the password entered in edittextpreference
in the preference summary.I have handled it logically to not update the summary as follows:
if (!preference.getKey().equals(EMAIL_PWD_KEY)
&& !preference.getKey().equals(PASSCODE_KEY))
{
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(),
""));
}
But when i click on 'OK' button of preference, the summary gets updated. Any idea on preventing it from updating ?
Upvotes: 0
Views: 995
Reputation: 64
I found a way to do this by implementing a class called EditTextPreferenceWithSummary which extends EditTextPreference and overriding some methods...
public class EditTextPreferenceWithSummary extends EditTextPreference {
public EditTextPreferenceWithSummary(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EditTextPreferenceWithSummary(Context context) {
super(context);
init();
}
@Override
protected View onCreateView(ViewGroup parent) {
updateSummary();
return super.onCreateView(parent);
}
public void updateSummary()
{
String value = "";
int variation = (this.getEditText().getInputType() & InputType.TYPE_MASK_VARIATION);
Boolean isPassword = ((variation == InputType.TYPE_NUMBER_VARIATION_PASSWORD)
||(variation == InputType.TYPE_TEXT_VARIATION_PASSWORD)
||(variation == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD));
if (isPassword)
{
value = this.getText().replaceAll(".", "*");
}
else
{
value = this.getText();
}
this.setSummary(value);
}
private void init() {
setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
EditTextPreferenceWithSummary pref = (EditTextPreferenceWithSummary) preference;
pref.updateSummary();
return true;
}
});
}
}
And my preference xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<PreferenceCategory
android:title="@string/pref_header_database">
<com.designit.api.preferences.EditTextPreferenceWithSummary
android:title="@string/field_database_server_ip"
android:defaultValue=""
android:summary="Set the IP adress of the server"
android:key="server_ip_address" />
<com.designit.api.preferences.EditTextPreferenceWithSummary
android:title="@string/field_database_server_port"
android:defaultValue=""
android:inputType="number"
android:summary="Set the port of the server"
android:key="server_port" />
<com.designit.api.preferences.EditTextPreferenceWithSummary
android:title="@string/field_database_name"
android:defaultValue=""
android:summary="Set the database name"
android:key="database_name" />
<com.designit.api.preferences.EditTextPreferenceWithSummary
android:title="@string/field_database_user"
android:defaultValue=""
android:summary="Set the database user"
android:key="database_user" />
<com.designit.api.preferences.EditTextPreferenceWithSummary
android:title="@string/field_database_password"
android:defaultValue=""
android:summary="Set the database password"
android:key="database_passwd"
android:inputType="textPassword"/>
</PreferenceCategory>
Hope this helps you. Cheers.
Upvotes: 1