Reputation: 7051
I keep seeing the tutorial on adding your own seekbar preference, but its not in my actual prefs.xml. Is there any way to have one in my main prefs screen or will I have to separate it.
Upvotes: 6
Views: 5265
Reputation: 23018
There seem to be 2 slider preferences by Google -
but I am not sure when they will be available.
You could use a custom inline SeekBarPreference by me (see the 2 sliders at the bottom):
My code is quite simple -
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);
}
}
<?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: 12