Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've resolved my issue by extending the <code>EditTextPrefence</code> class and overriding <code>setText</code> and <code>getText</code></p> <p>this is based on the answer I found <a href="https://stackoverflow.com/questions/5858790/process-the-value-of-preference-before-save-in-android">here</a></p> <pre><code>package com.example.overrides; import android.content.Context; import android.preference.EditTextPreference; import android.util.AttributeSet; public class SanitizedEditTextPreference extends EditTextPreference { public SanitizedEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SanitizedEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } public SanitizedEditTextPreference(Context context) { super(context); } @Override public String getText() { String value = super.getText(); return StringUtil.sanitize(value); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { super.setText(restoreValue ? getPersistedString(null) : (String) defaultValue); } @Override public void setText(String text) { if (StringUtil.isStringBlank(text)) { super.setText(null); return; } super.setText(StringUtil.sanitize(text)); } } </code></pre> <p>I simplified my previous code to:</p> <pre><code>/** * A preference value change listener that updates the preference's summary * to reflect its new value. */ private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { String stringValue = value.toString(); if (preference instanceof ListPreference) { // For list preferences, look up the correct display value in // the preference's 'entries' list. ListPreference listPreference = (ListPreference) preference; int index = listPreference.findIndexOfValue(stringValue); // Set the summary to reflect the new value. preference.setSummary( index &gt;= 0 ? listPreference.getEntries()[index] : null); } else if (preference.getKey().equals("text_default_album")) { stringValue = StringUtil.sanitize(stringValue); preference.setSummary(stringValue); } else { // For all other preferences, set the summary to the value's // simple string representation. preference.setSummary(stringValue); } return true; } }; </code></pre> <p>and in the <code>pref.xml</code> file it is used like:</p> <pre><code>&lt;com.example.overrides.SanitizedEditTextPreference android:key="text_default_album" android:title="@string/pref_title_default_album" android:defaultValue="@string/pref_default_album" android:selectAllOnFocus="true" android:inputType="text" android:capitalize="none" android:singleLine="true" android:maxLines="1" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload