title: How to set RingtonePreference value from code?
body: <p>I've the following Preference:</p>
  
  <pre><code>&lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt;
     &lt;RingtonePreference
      android:showDefault="true"
      android:showSilent="true"
      android:title="@string/feed_alert_ringtone"
      android:ringtoneType="ringtone|notification|alarm|all"
      android:key="alertringtone"
      android:persistent="false"&gt;
     &lt;/RingtonePreference&gt;     
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p>When a user changes this preference, I manually save the Uri to a database:</p>
  
  <pre><code>public boolean onPreferenceChange(Preference pref, Object change) {
      String ringtone = change.toString();
  
      // save it to a db
      ...
  
      return true;
  }
  </code></pre>
  
  <p>My problem is, when the user closes and then gets back to the <code>PreferenceScreen</code>, the default Value of the <code>RingtonePreference</code> is always <code>Silence</code>. Of course, I have to set the value manually.</p>
  
  <p>Tried this one in my <code>PreferenceActivity</code>:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      addPreferencesFromResource(R.xml.settings); // the XML above
  
      String database_ringtone = ... // get the string saved above from db
                                     // and according to some parameters passed
                                     // to the intent
  
      Preference ringtone = findPreference("alertringtone");
      ringtone.setDefaultValue( database_ringtone );
  
      // also tried:
      ringtone.setDefaultValue( Uri.parse(database_ringtone) );
  }
  </code></pre>
  
  <p>Unfortunatly the preference stays at "Silence" (which means empty). I thought, that when the <code>Preference</code> is not <code>persistent</code> the default value will be used.</p>
  
  <p>I cannot make it <code>persistent</code>, because the preference is used for multiple items and the data I get from DB changes according to some extra data passed to the intent.</p>
  
  <p>Nevertheless, I do not want to set the default value, but the current value I get from database. Thought this might be a workaround. So any other way would be good, too.</p>
  
  <p>Double-checked saving and retrieving from DB is working, so that's not the problem.</p>
  
  <p>How can I set the <code>RingtonePreference</code> value from code?</p>
  
  <hr>
  
  <p>Edit: Here's a little explanation what I want to do, maybe there's another way.</p>
  
  <p>I've several menu entries (count varies from user to user), for which the user can select a ringtone for. Selecting the ringtone works with the xml above, the ID of the menu entry is passed with an <code>Intent</code> to the <code>PreferenceActivity</code>. The URI of the ringtone is then stored in the database.</p>
  
  <p>When one of the entries changes, an alarm is played according to the ringtone-selection the user made. So he knows which one changed. No problem so far.</p>
  
  <p>The problem is, when the user wants to change the ringtone for an entry he already defined one for. If the user selected ringtone Foo before, when clicking on the <code>RingtonePreference</code>, the ringtone Foo should be preselected already. This is no serious error (more like a glitch), but still very annoying.</p>
  
  <p>As far as I can tell, there is no way to preselect a default <code>RingtonePreference</code> from code? Or am I doing it "just wrong"?</p>
  
  <hr>
  
  <p>Edit 2: Okey, I think there isn't a way to do this. Very weird, the android core. Allows us to use a <code>Preference</code> and get values from it but not to put values back in. Must have been a real hurry. I'm glad they allowed us to check a <code>CheckBoxPreference</code> from code...</p>