Reputation: 51
Please tell me how to save SeekBar progress in Preferences in Android and also how to get that saved value to be shown on seekbar whenever user will open it again.
Upvotes: 2
Views: 5175
Reputation: 1411
I am giving you the xml and java code to save the state of a SeekBar along with a TextView which shows the value of that SeekBar using SharedPreferences class.
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="30sp"
tools:text="0" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
Java code:
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private SeekBar seekBar;
private int seekBarProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
textView.setText(String.valueOf(i));
saveData();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
loadData();
updateViews();
}
public void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences("my_preference", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("my_seekBar", seekBar.getProgress());
editor.apply();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences("my_preference", MODE_PRIVATE);
seekBarProgress = sharedPreferences.getInt("my_seekBar",0);
}
public void updateViews() {
seekBar.setProgress(seekBarProgress);
textView.setText(String.valueOf(seekBarProgress));
}
}
Upvotes: 1
Reputation: 51
public class MainActivity extends Preference implements OnSeekBarChangeListener { private SeekBar mSeekBar; private int mProgress;
public MainActivity(Context context) {
this(context, null, 0);
}
public MainActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MainActivity(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_seekbar);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
mSeekBar.setProgress(mProgress);
mSeekBar.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser)
return;
setValue(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// not used
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// not used
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
}
public void setValue(int value) {
if (shouldPersist()) {
persistInt(value);
}
if (value != mProgress) {
mProgress = value;
notifyChanged();
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 0);
}
}
Upvotes: 0
Reputation: 23018
Try my simple but complete example with slider preference at GitHub:
SeekBarPreference.java (saves SeekBar progress as integer value):
public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
private SeekBar mSeekBar;
private int mProgress;
public SeekBarPreference(Context context) {
this(context, null, 0);
}
public SeekBarPreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_seekbar);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
mSeekBar.setProgress(mProgress);
mSeekBar.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser)
return;
setValue(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// not used
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// not used
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
setValue(restoreValue ? getPersistedInt(mProgress) : (Integer) defaultValue);
}
public void setValue(int value) {
if (shouldPersist()) {
persistInt(value);
}
if (value != mProgress) {
mProgress = value;
notifyChanged();
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 0);
}
}
preference_seekbar.xml (the layout file with SeekBar and TextView):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<SeekBar
android:id="@+id/seekbar"
android:layout_width="0dp"
android:layout_weight="80"
android:layout_height="wrap_content" />
<TextView
android:id="@android:id/summary"
android:layout_width="0dp"
android:layout_weight="20"
android:layout_height="wrap_content" />
</LinearLayout>
Upvotes: 2
Reputation: 9044
initialize a SharedPreferences object:
SharedPreferences pre = getSharedPreferences("pre_name", Context.MODE_PRIVATE);
Add a value in preferences:
pre.edit().putInt("KEY_PROGRESS_VALUE", 30).commit();
Load a value from preferences:
int progress = pre.getInt("KEY_PROGRESS_VALUE",0); // 0: default value
And then set progress bar's progress:
progrssBar.setProgress(progress);
Upvotes: 0
Reputation: 17037
Save the progress
SharedPreference mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor mEditor = mSharedPrefs.edit();
int mProgress = mSeekBar.getProgress();
mEditor.putInt("mMySeekBarProgress", mProgress).commit();
and than you can do this to get it :
int mProgress = mSharedPrefs.getInt("mMySeekBarProgress", 0);
mSeekBar.setProgress(mProgress);
Upvotes: 0