title: Android: Unable to get preference values from settings.xml
body: <p>I have a settings.xml in my res folder to manage user settings. I load this into a fragment. I require to store these settings to a backend service for this I want to know what values have been by the user or whenever user changes a setting I want to know so that I update the server about the settings.</p>
  
  <p>I have tried many approaches as shown in the code below but I am unable to read the value. I tried putting Toast and log statements to check the listeners but nothing is appearing which indicates that listener is not getting called. Am I doing something wrong?</p>
  
  <p>UserSettingFragment.java</p>
  
  <pre><code>public class UserSettingFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      addPreferencesFromResource(R.xml.settings);
      //Method 1
      PreferenceScreen shared = getPreferenceScreen();
      shared.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  
          @Override
          public boolean onPreferenceChange(Preference preference, Object newValue) {
              // TODO Auto-generated method stub
              Toast.makeText(getActivity().getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
              Log.d("preference", "change");
              return true;
          }
      });
  
      final SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
  
      Preference gender = (Preference) findPreference("gender");
      //Method 2
      // Get the custom preference
      Preference mypref = (Preference) findPreference("mypref");
      mypref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  
          @Override
          public boolean onPreferenceChange(Preference preference, Object newValue) {
              // TODO Auto-generated method stub
              String highScore = sharedPref.getString("gender", "Male");
              Toast.makeText(getActivity().getApplicationContext(), "Changed to " + highScore, Toast.LENGTH_LONG).show();
              Log.d("preference", "change");
              return false;
          }
      });
  } //Method 3
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
          String key) {
      // TODO Auto-generated method stub
      Toast.makeText(getActivity().getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>settings.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p>
       
          </p>
  
  <pre><code>    &lt;SwitchPreference 
          android:key="gender"
          android:title="I am"
          android:switchTextOn="Female"
          android:switchTextOff="Male"
           /&gt;
  
  &lt;/PreferenceCategory&gt;
  </code></pre>
  
  <p></p>