title: EditTextPreference validate data before saving
body: <p>I have a method to validate a EditTextPreference. My method is executed after the confirmation of data by implementing the onSharedPreferenceChanged class.</p>
  <p>However, only occurs after you confirm the information. I would perform the check without closing the dialog box. And if ok then close or keep open for user to enter the data correctly.</p>
  <p>If it's not possible, I would reopen the dialog box if the validation is false.</p>
  <h1>SettingsActivity.java</h1>
  <pre><code>class SettingsActivity extends PreferenceActivity implements
              OnSharedPreferenceChangeListener {
  
      protected static final String TAG = &quot;SettingsActivity&quot;;
      private SharedPreferences prefs;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          prefs = PreferenceManager.getDefaultSharedPreferences(this);
          prefs.registerOnSharedPreferenceChangeListener(this);
      }
  
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
              String key) {
          // TODO Auto-generated method stub
          SharedPreferences.Editor prefEditor = prefs.edit();
          
          if(key.equals(&quot;pref_Url&quot;)){
              String url = prefs.getString(key, &quot;&quot;);
              boolean response = (!new ConnectUtils().isConnected(this, url));
              
              if(!response){
                  prefEditor.putString(key, Config.SERVER_URL_DEF_VALUE);
                  prefEditor.commit();
                  reload();
                  Toast.makeText(this,R.string.msgToast_server_url_invalid,Toast.LENGTH_SHORT).show();
              }
              
          }else if (key.equals(&quot;pref_Id&quot;)){
              String url = Config.SERVER_URL_ID;
              boolean reponse = (!new ConnectUtils().isConnected(this,url));
              
              if(!reponse){
                  prefEditor.putString(key,Config.ID_DEF_VALUE);
                  prefEditor.commit();
                  reload();
                  Toast.makeText(Config.getContext(), R.string.msgToast_Id_invalid, Toast.LENGTH_SHORT).show();               
              }           
          }
      }   
      private void reload(){
          startActivity(getIntent()); 
          finish();       
      }
  }
  </code></pre>