Search code examples
androidkotlinandroid-seekbar

How to save SeekBar thumb position after setting value to its progress?


I'm trying to get worked a segmented SeekBar for font site with step of 2. It's working, but I can't keep thumb position, it is always on 0.

private fun fontSize() {
        val view = LayoutInflater.from(this).inflate(R.layout.font_size_layout, null)
        size = view.findViewById(R.id.font_size_sb)
        val preference = PrefManager(this)
        font = view.findViewById(R.id.font_size_tv)
        font.textSize = preference.getFontSize().toFloat()
        font.text = preference.getFontSize().toString()

        size.apply {
            max = (36 - 12) / 2
            progress = preference.getFontSize()
            setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    updateFontSize(12 + (progress * 2))
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })
        }
        AlertDialog.Builder(
            this,
            R.style.AlertDialogSlider
        ).apply {
            setView(view)
            create()
            show()
        }
    }

    private fun updateFontSize(i: Int) {
        note.textSize = i.toFloat()
        font.text = i.toString()
        font.textSize = i.toFloat()
        preference.saveFontSize(i)
    }

My preference class PrefManager:

class PrefManager(private var context: Context) {
    fun saveFontSize(size: Int) {
        context.getSharedPreferences("font_size", AppCompatActivity.MODE_PRIVATE).edit().apply {
            putInt("fontSize", size)
            apply()
        }
    }

    fun getFontSize(): Int {
        return context.getSharedPreferences("font_size", AppCompatActivity.MODE_PRIVATE)
            .getInt("fontSize", -1)
    }

For example, I set the font size to 18:

Chosen font size

That's good and works for me, but when I want to change the size one more time, the SeekBar's position does not stay there, where I left it lastly. It goes to end:

Thumb went to end

How can I keep the position?


Solution

  • After spending a lot of time, I finally found a solution and it's very simple:

    class PrefManager(private var context: Context) {
        fun saveFontSize(size: Int, progress: Int) {
            context.getSharedPreferences("font_size", AppCompatActivity.MODE_PRIVATE).edit().apply {
                putInt("fontSize", size)
                putInt("progress", progress)
                apply()
            }
        }
    
        fun getFontSize(): Int {
            return context.getSharedPreferences("font_size", AppCompatActivity.MODE_PRIVATE)
                .getInt("fontSize", 18)
        }
        fun getProgress(): Int {
            return context.getSharedPreferences("font_size", AppCompatActivity.MODE_PRIVATE)
                .getInt("progress", 3)
        }
    }
    

    And piece of code for SeekBar:

    size.apply {
                progress = preference.getProgress()
                setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(
                        seekBar: SeekBar?,
                        progress: Int,
                        fromUser: Boolean
                    ) {
                        val fontSize = 12 + (progress * 2)
                        note.textSize = fontSize.toFloat()
                        font.text = fontSize.toString()
                        preference.saveFontSize(fontSize, progress)
                    }
    
                    override fun onStartTrackingTouch(seekBar: SeekBar?) {
                    }
    
                    override fun onStopTrackingTouch(seekBar: SeekBar?) {
    
                    }
                })
            }
    

    I had just to keep saved the original values of progress within the onProgressChanged(). That's all!