Continue work on CustomSeekBarPreference

This commit is contained in:
Patryk Michalik
2021-03-03 12:38:27 +01:00
parent 06afdc137c
commit 5ad285bd15
4 changed files with 85 additions and 5 deletions
@@ -0,0 +1,42 @@
<?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="72dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="TextView"
android:textAppearance="?attr/textAppearanceBody1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/value_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="@color/material_on_surface_emphasis_medium" />
</LinearLayout>
</LinearLayout>
+2
View File
@@ -3,5 +3,7 @@
<declare-styleable name="CustomSeekBarPreference">
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
<attr name="snapEvery" format="integer" />
<attr name="asPercentages" format="boolean" />
</declare-styleable>
</resources>
+3 -1
View File
@@ -23,7 +23,9 @@
android:key="pref_dock_label_size"
android:title="Label Size"
app:showSeekBarValue="true"
lawnchair:snapEvery="10"
lawnchair:minValue="50"
lawnchair:maxValue="150" />
lawnchair:maxValue="150"
lawnchair:asPercentages="true" />
</PreferenceScreen>
@@ -1,22 +1,35 @@
package ch.deletescape.lawnchair.settings.ui.preferences
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.widget.SeekBar
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import androidx.preference.SeekBarPreference
import com.android.launcher3.R
class CustomSeekBarPreference(context: Context, attrs: AttributeSet? = null) : SeekBarPreference(context, attrs), Preference.OnPreferenceChangeListener {
class CustomSeekBarPreference(context: Context, attrs: AttributeSet? = null) : Preference(context, attrs), Preference.OnPreferenceChangeListener {
private val minValue: Int
private val maxValue: Int
private val snapEvery: Int
private val asPercentages: Boolean
private var displayedValue: Int = 0
private var realValue: Double = 0.0
init {
widgetLayoutResource = R.layout.custom_seek_bar_preference
layoutResource = R.layout.custom_seek_bar_preference
context.obtainStyledAttributes(attrs, R.styleable.CustomSeekBarPreference).apply {
minValue = this.getInt(R.styleable.CustomSeekBarPreference_minValue, 10)
minValue = this.getInt(R.styleable.CustomSeekBarPreference_minValue, 0)
maxValue = this.getInt(R.styleable.CustomSeekBarPreference_maxValue, 100)
snapEvery = this.getInt(R.styleable.CustomSeekBarPreference_snapEvery, 1)
asPercentages = this.getBoolean(R.styleable.CustomSeekBarPreference_asPercentages, false)
recycle()
}
}
@@ -24,11 +37,32 @@ class CustomSeekBarPreference(context: Context, attrs: AttributeSet? = null) : S
override fun onBindViewHolder(view: PreferenceViewHolder?) {
super.onBindViewHolder(view)
onPreferenceChangeListener = this
Log.i(null, "$minValue")
val seekBar: SeekBar = view?.findViewById(R.id.seek_bar) as SeekBar
val valueView: TextView = view.findViewById(R.id.value_view) as TextView
seekBar.max = (maxValue - minValue) / snapEvery
seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
displayedValue = progress * snapEvery + minValue
realValue = if (asPercentages) displayedValue.toDouble() / 100 else displayedValue.toDouble()
valueView.text = if (asPercentages) "$displayedValue%" else "$displayedValue"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
}
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
TODO("Not yet implemented")
return true
}
}