androidkotlinandroid-widgetrangeseekbar

Kotlin - Range seekbar library


i'm using syedowaisali's Range Seekbar, and i'm having trouble getting my TextView to change according to the thumb's position on the bar.

I have the following in my Activity's onCreate:

    val rangeSeekbar = CrystalRangeSeekbar(this)
    rangeSeekbar.setOnRangeSeekbarChangeListener { minValue, maxValue ->
        tv_rent_time_range.text = maxValue.toString()
    }

tv_rent_time_range loads with the maxValue but it doesn't change when i move the right thumb

In his documentation (in Java) it's suppose to look like this:

rangeSeekbar.setOnRangeSeekbarChangeListener(new OnRangeSeekbarChangeListener() {
    @Override
    public void valueChanged(Number minValue, Number maxValue) {
        tvMin.setText(String.valueOf(minValue));
        tvMax.setText(String.valueOf(maxValue));
    }
});

I have a feeling i'm setting up the setOnRangeSeekbarChangeListener wrong in Kotlin


Solution

  • You are creating new object (val rangeSeekbar = CrystalRangeSeekbar(this)) instead of using view from your inflated layout. You can just use:

    rangeSeekbar.setOnRangeSeekbarChangeListener { minValue, maxValue ->
        // Do something there  
    }
    

    For SeekBar from Android SDK you can receive updates from setOnSeekBarChangeListener.

    For CrystalRangeSeekbar from Android SDK you can receive updates from setOnRangeSeekbarChangeListener.

    import android.os.Bundle
    import android.support.v7.app.AppCompatActivity
    import android.widget.SeekBar
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            setupUi()
        }
    
        private fun setupUi() {
            // Regular SeekBar (from Android SDK)
            seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    // Update text here
                }
    
                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                }
    
                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                }
            })
    
            crystal_range_seek_bar.setOnRangeSeekbarChangeListener { minValue, maxValue ->
                // Update text here
            }
        }
    }