i wrote a simple PreferenceScreen, looks as follows:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getTitle().equals(getString(R.string.settings_diverse_about))) {
// TODO open about dialog
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
private static String PATTERN_EMAIL = "[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
private static String PATTERN_TWITTER = "[a-zA-Z0-9_-]+";
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefTitle = preference.getTitle().toString();
// phone will be fully handled by inputType
String newStr = newValue.toString();
if (prefTitle.equals(getString(R.string.settings_contact_email))) {
return newStr.trim().matches(PATTERN_EMAIL);
} else if (prefTitle.equals(getString(R.string.settings_contact_twitter))) {
return newStr.trim().matches(PATTERN_TWITTER);
}
return true;
}
The problem is, the onPreferenceChange method gets never invoked. Furthermore, what do you think about the way of validating the attributes? I think it's not really optimal.
preference.getTitle().equals(getString(R.string.settings_diverse_about))