0

I'm trying to put a dynamic text, which when dragging the seekbar, you can see the value while dragging the bar. But I'm not getting any success.

I tried some post here in stackoverflow but I could not do anything, it follows the code I worked on:

class AccountActivity : LoggedActivity<ViewAccountBinding, AccountViewModelKotlin>() {

    // BASE METHODS
....

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var _testText = TextView(this)
        _testText.setTextColor(resources.getColor(R.color.black_75))

        var currentValue : Int = binding.seekbarAccount.progress

        binding.seekbarAccount.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, progress: Int, p2: Boolean) {
                currentValue = progress

                //just to show dynamic text above thumb of seekbar
                var value : Int = (progress * (binding.seekbarAccount.width - 2 * binding.seekbarAccount.thumbOffset))/binding.seekbarAccount.max
                _testText.text = ""+progress.toString()
                _testText.x = (binding.seekbarAccount.x + value +  binding.seekbarAccount.thumbOffset)/2
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
                //
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                viewModel.fontSize = currentValue
                Toast.makeText(this@AccountActivity,currentValue.toString(),Toast.LENGTH_SHORT).show()
            }
        })
}

XML:

                    <SeekBar
                        android:id="@+id/seekbarAccount"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/margin_large"
                        android:alpha="0.75"
                        android:max="25"
                        android:min="7"
                        android:progress="@{viewModel.fontSize}"
                        android:progressDrawable="@drawable/progress_seek_bar_settings"
                        android:splitTrack="false"
                        android:thumb="@drawable/thumb_seekbar_settins" />
  • 1
    Make a custom view extending `SeekBar` that draws the text above thumb. See this answer: https://stackoverflow.com/a/10722881/5288316 – Nicolas Dec 21 '17 at 21:29
  • @NicolasMaltais I've seen this post, but how am I going to extend it if I'm already in another class. I did not quite understand how to do within my placed example. Thank you! –  Dec 22 '17 at 10:38
  • You need to create another class for the custom view and then use it in the layout file with `com.example.CustomView` – Nicolas Dec 22 '17 at 13:15

0 Answers0