Home > Mobile >  I am using an alertDialog box with a seekbar in Kotlin. The seekbar works but I can't update th
I am using an alertDialog box with a seekbar in Kotlin. The seekbar works but I can't update th

Time:12-05

**I am trying to use an alertDialog box using a seekbar. The seekbar works but I can't update the textView - (tvPlayers). Originally I just used an editText box and that worked but it would be nice if I could use an seekbar.

The variable numberOfPlayers stores the seekbar progress once the positive button is selected but I can't track the positions as the seekbar thumb is moved.

I am using Kotlin and viewBinding with Android Studio 3.1

edit_textnumberplayers

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@ id/tvPlayers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:gravity="center"
        android:text="0"
        android:textSize="16sp" />

    <SeekBar
        android:id="@ id/etNumberOfPlayers"
        style="@android:style/Widget.DeviceDefault.SeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:max="10"
        android:min="1"
        android:progress="1"
        android:progressTint="#0710B8"
        android:thumb="@drawable/ic_baseline_person_add_alt_1_24" />


    <!--<EditText
        android:id="@ id/etNumberOfPlayers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:hint="Select number of players"
        android:inputType="number"
        android:textSize="16sp" />-->


</LinearLayout>

initialSetupScreen.kt

    private fun chooseNumberOfPlayers() {
        val builder = AlertDialog.Builder(this)
        val inflater = layoutInflater
        val dialogLayout = inflater.inflate(R.layout.edit_text_number_players, null)
        val seekBar = dialogLayout.findViewById<SeekBar>(R.id.etNumberOfPlayers)

        with(builder) {
            setTitle("How many players do you need")

            binding2.tvPlayers.text = seekBar.progress.toString()

            setPositiveButton("OK") { dialog, which ->
                numberOfPlayers = seekBar.progress
                // Show the correct number of enter name boxes.
                showPlayerNameTextBoxes()
            }

            setNegativeButton("Cancel") { dialog, which ->
                Log.d("Main", "Negative button clicked")
                hidePlayerNameTextBoxes()
                finish() // Return to MainMenu screen.
            }
            setView(dialogLayout)
            show()
        }
    }

CodePudding user response:

You should update textView when Seekbar's value changes.
For example:

with(builder) {
    setTitle("How many players do you need")

    seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seek, progress, fromUser) {
          binding2.tvPlayers.text = seekBar.progress.toString()
        }
  
        override fun onStartTrackingTouch(seek) {}
        override fun onStopTrackingTouch(seek) {}
    })

    setPositiveButton("OK") { dialog, which ->
        numberOfPlayers = seekBar.progress
        // Show the correct number of enter name boxes.
        showPlayerNameTextBoxes()
    }
    setNegativeButton("Cancel") { dialog, which ->
        Log.d("Main", "Negative button clicked")
        hidePlayerNameTextBoxes()
        finish() // Return to MainMenu screen.
    }
    setView(dialogLayout)
    show()
}
  • Related