Seekbar progress update
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).
SQLiteNoob
Updated on June 13, 2022Comments
-
SQLiteNoob almost 2 years
Background: I have one activity, with a Textview and a button. Clicking the button runs
SetValue()
which opens a dialog that contains a Seekbar, which in turn sets the value for the textview. Here's the code:public void SetValue(View view){ ShowDialog(); } public void ShowDialog() { final AlertDialog.Builder popDialog = new AlertDialog.Builder(this); final SeekBar seek = new SeekBar(this); int value = 0; try { value = Integer.parseInt(tv.getText().toString()); } catch(NumberFormatException nfe) { System.out.println("Could not parse " + nfe); } seek.setProgress(value); seek.setMax(100); popDialog.setView(seek); seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 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) { // TODO Auto-generated method stub } }); // Button popDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); popDialog.create(); popDialog.show(); }
So it works fine, displaying the initial textview progress, and lets me update it with the seekbar.
Problem: After initially setting the value, if I try to use the method again to change the text, the progress on the seekbar starts at 0 every time, when it should be at the previously selected value/current textview value.
Also, I can set a max value for the bar, can I set a min value? Ie -100? How do I set a range for the seekbar?
Thanks for all advice!
Update 1: I initialized the variable at the start of the class, so that eliminated the
int value = 0;
line, but the problem persists - the seekbar always opens with the progress at the original text value of the textview. -
SQLiteNoob about 10 yearsIt works well, and is a fairly easy way to do it. I'll post a few questions about turning this into a separate class, and getting it into a database so it's there after closing the app and opening it back up.
-
Adam S about 10 yearsSettings stored in
SharedPreferences
will persist across app instances. They'll only disappear if you callEditor.clear()
or if you uninstall the app. -
SQLiteNoob about 10 yearsReally? Using the displayed code, when I leave the app and return, the data persists, but if I close it via the "Recents" button, the data resets when I restart the app.
-
Adam S about 10 yearsThat's really odd, I'm not sure what's going on there as this should definitely persist.