First of all, you need to persist your value when the user finishes 'seeking' the SeekBar
. We do this in onStopTrackingTouch
:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
//Update textview value
tv.setText("Value : " + progress);
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// Update your preferences only when the user has finished moving the seek bar
SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
// Don't forget to call commit() when changing preferences.
prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}
Now you want to display this persisted value whenever the dialog is created - so you'll need to retrieve it. You can do this like this:
int value = 0;
SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 0); // 0 is default
seek.setProgress(value);
// You also need to update your TextView to show the saved value
tv.setText("Value : " + value);
You can wrap the access to the SharedPreferences
in a class; this is one way to ensure you're using the same preferences file name ("mySharedPrefsFilename"
in this example) and the same preference keys to store/retrieve the value ("seekbarValue"
in this example).