androidarraysandroid-studiokotlinandroid-seekbar

Handle screen rotation progress without losing data by using Seekbar - Android Kotlin


I am newbie on android studio, i had face problem on when i rotation the screen the seek bar progress will be reset it to 0, that i had try using saved Instance State but the progress still reset it , can anyone help me give me some solution to solve it ?

I get this error Type mismatch: inferred type is String? but Int was expected

Thank you all .Hope will get back soon ya

above is the code at MainActivity.kt

//the bottom one
var no:Int=0
private val SEEKBAR ="seekbar"

//Inside the onCreate
    if (savedInstanceState !=null)       {
        val value = savedInstanceState.getString(SEEKBAR)
        seekbar1.progress=value
        no= value?.toInt() ?:0
    }

//after the onCreate
override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)

val seekbar1 = seekbar1.progress.toString()
outState.putString(SEEKBAR,seekbar1)
}

Solution

  • You are passing string to the progress function of seekbar. We need to pass integer. So, simply use seekbar_weight.progress = value.toInt()

    UPDATE AFTER GOING THROUGH YOUR CODE:

    In your code, in the setVariable() function, you are setting some UI's according to the Units user picked.

    While doing that, you are setting your progress of the seekbar to 0. And this function is being called after the progress value is restored from onRestoreInstateState. After it gets restored, you are again setting it back to zero from setVariable() function.

    So, you just need to remove 8 lines (which are used to set seekbar progress and text to show seekbar progress) from your setVariable() function.

    So, your setVariable() function should look like:

        private fun setVariable() {
        //Set the text of units and the max progress
        spinner_units.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                if (position == 0) {
                    text_weight.text = "Weight (kg)"
                    text_height.text = "Height (cm)"
                    seekbar_weight.max = 450
                    seekbar_height.max = 250
                    number_picker_age.value = 2
                    bmi_meter.speedTo(0F)
                } else if (position == 1) {
                    text_weight.text = "Weight (ibs)"
                    text_height.text = "Height (in)"
                    seekbar_weight.max = 770
                    seekbar_height.max = 80
                    number_picker_age.value = 2
                    bmi_meter.speedTo(0F)
                }
    
            }
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
    }
    

    UPDATE 2: AFTER UPDATED REQUIREMENT

    For your updated requirement here you can go: Bring back those 8 lines those were deleted from setVariable() method.

    In your strings.xml file, add one item 'Select' in the string array for the spinner.

    <string-array name="units">
            <item>Select</item>
            <item>Metric</item>
            <item>Imperial</item>
    </string-array>
    

    In MainActivity, override onPause() method and set the spinner to position 0.

    override fun onPause() {
        super.onPause()
        spinner_units.setSelection(0)
    }
    

    That should solve your problem. If not please comment back.