Search code examples
javaandroidseekbar

Android seekbar value change button


project's problem Image:

project's problem Image

Hi. I want to know about this problem with these two buttons. First of all, I emphasized two buttons. Left button is Decrease button and right button is Increase button. And the problem is... If all values are the maximum value. Clicking the Increase button does not change the value. However, if I click the increase button two or three times and then click the decrease button to decrease the value again, the value does not change. To decrease the value, I must press the decrease button as much as I press the increase button. :(

When I first encountered this problem, I thought it was caused by the overlapping call of OnClickListener. So I tried to avoid calling in duplicate if the value was the maximum, but I couldn't solve the problem. I ask questions because I've been looking for people who have had the same problem with me for about a week but haven't found a solution. Please. I want to go home.


Solution

  • Kotlin

    Your logic should be like this for increaseButton

            increaseButton.setOnClickListener {
            if(seekBar.progress > MAX_LIMIT) {
                seekBar.progress = MAX_LIMIT
                return@setOnClickListener
            }
            if (seekBar.progress == MAX_LIMIT) {
                // you can show notification that you are reach limit
                return@setOnClickListener
            }
            seekBar.progress = seekBar.progress + 1
        }
    

    And same logic for decreaseButton

            decreaseButton.setOnClickListener {
            if(seekBar.progress < MIN_LIMIT) {
                seekBar.progress = MIN_LIMIT
                return@setOnClickListener
            }
            if (seekBar.progress == MIN_LIMIT) {
                // you can show notification that you are reach limit
                return@setOnClickListener
            }
            seekBar.progress = seekBar.progress - 1
        }