package de.tudarmstadt.informatik.hostage.ui; import de.tudarmstadt.informatik.hostage.R; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.widget.Toast; /** * SettingsActivity creates the settings defined in /xml/preferences.xml. * @author Lars Pandikow. */ public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); //Set the value of the preference as the summary for the preference Preference pref = findPreference("pref_external_location"); EditTextPreference etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); //Set the value of the preference as the summary for the preference pref = findPreference("pref_upload_server"); etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); } protected void onResume() { // register a listener to catch preference changes super.onResume(); getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); } protected void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // Check which preference has been changed if(key.equals("pref_external_location")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; String path = etp.getText(); //Check if the Path is valid if(!path.startsWith("/")) path = new String("/").concat(path); if(!path.endsWith("/")) path = path.concat(new String("/")); if (!path.matches("/(([a-zA-Z_0-9])+/)*")){ Toast.makeText(getApplicationContext(), "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show(); path = "/"; } pref.setSummary(path); } else if(key.equals("pref_upload_server")){ Preference pref = findPreference(key); EditTextPreference etp = (EditTextPreference) pref; pref.setSummary(etp.getText()); } } }