javascriptandroidandroid-seekbarchangelistenerrangeseekbar

How to call different methods when min and max values are changed in range seek bar


I have a range seek bar in my app. I need to call a method by passing the min value of the bar only if the value is changed and also to call another method by passing the max value if the max value is changed. Here is my code so far.

rangeSeekbar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
        @Override
        public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                //call this if min value is changed
                mywebView.loadUrl("javascript:setStart(" + minValue.intValue() + ")");

                //call this if max value is changed
                mywebView.loadUrl("javascript:setEnd(" + maxValue.intValue() + ")");
            }

            Toast.makeText(getApplicationContext(), minValue + "-" + maxValue, Toast.LENGTH_LONG).show();
        }
    });

The problem is when only one value changes, both methods are called. How can I differentiate the method calls? Any ideas are welcome.


Solution

  • You can declare a field variable to save the value you have changed:

    int min ,max;
    rangeSeekbar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                    if(minValue.intValue!=min){
                        //call this if min value is changed
                        mywebView.loadUrl("javascript:setStart(" + minValue.intValue() + ")");
                        min = minValue.intValue;
                    }
    
                    if(maxValue.intValue!=max){
                        //call this if max value is changed
                        mywebView.loadUrl("javascript:setStart(" + maxValue.intValue() + ")");
                        max = maxValue.intValue;
                    }    
    
            }
    
    });