Skip to content

Instantly share code, notes, and snippets.

@ibrahimsn98
Created October 27, 2018 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibrahimsn98/3e816dd51c6a28c7a7e5bc42649fd6da to your computer and use it in GitHub Desktop.
Save ibrahimsn98/3e816dd51c6a28c7a7e5bc42649fd6da to your computer and use it in GitHub Desktop.
Android Custom SeekBar Preference
<?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="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textSize="15sp"/>
<TextView
android:id="@android:id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="13sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/currentValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorSecondaryText"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
class SeekBarPreference(context: Context, attrs: AttributeSet) :
Preference(context, attrs, 0) {
private var max = 0
private var current = 0
init {
widgetLayoutResource = R.layout.preference_seekbar
layoutResource = R.layout.preference_seekbar
val ta = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference)
max = ta.getInt(R.styleable.SeekBarPreference_max, 0)
ta.recycle()
}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val seekBar: SeekBar = holder.findViewById(R.id.seekBar) as SeekBar
val currentValue: TextView = holder.findViewById(R.id.currentValue) as TextView
seekBar.max = max
seekBar.progress = 3
currentValue.text = "$current/$max"
seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
currentValue.text = "$progress/$max"
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
persistInt(seekBar.progress)
}
})
}
override fun onGetDefaultValue(a: TypedArray, index: Int): Int {
return a.getString(index).toInt()
}
override fun onSetInitialValue(restorePersistedValue: Boolean, defaultValue: Any?) {
super.onSetInitialValue(restorePersistedValue, defaultValue)
current = if (restorePersistedValue) getPersistedInt(0) else defaultValue as Int
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment